2016-10-14 2 views
0

세포의 버튼에 문제가 있습니다. 내 tableView에서 각 행은 이미지, 일부 레이블 및 버튼으로 구성됩니다. 버튼에 확인 표시가 있습니다. 클릭하면 버튼의 이미지가 변경됩니다. 문제는 다른 단추의 이미지가 이유없이 변경된다는 것입니다. 이 실수는 셀이 다시 사용되기 때문에 발생합니다.세포 재사용이 잘되지 않습니다 - TableView

TableViewCell에서 prepareForReuse 메서드를 사용하려고했지만 아무 일도 발생하지 않았습니다. 나는 또한 selectedRowAt으로 시도했지만 어떤 결과도 없었습니다. 도와주세요.

이미지 1 :

Image 1

이미지 2 :

Image 2

이 내 custom Cell:

override func prepareForReuse() { 
    if checkStr == "uncheck"{ 
     self.checkBook.setImage(uncheck, for: .normal) 
    } else if checkStr == "check"{ 
     self.checkBook.setImage(check, for: .normal) 
    } 
} 

func isPressed(){ 
    let uncheck = UIImage(named:"uncheck") 
    let check = UIImage(named: "check") 
    if self.checkBook.currentImage == uncheck{ 
     checkStr == "check" 
    } else self.checkBook.currentImage == check{ 
     checkStr == "uncheck" 
    } 
} 
내 FUNC입니다 내 tableView에서

:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    let selectedCell: ListPropertyUserCell = tableView.cellForRow(at: indexPath) as! ListPropertyUserCell 
    let uncheck = UIImage(named:"uncheck") 
    let check = UIImage(named: "check") 
    if selectedCell.checkBook.imageView?.image == uncheck{ 
     selectedCell.checkStr = "check" 
    } else if selectedCell.checkBook.imageView?.image == check{ 
     selectedCell.checkStr = "uncheck" 
    } 
} 
+0

셀 자체가 아닌 테이블보기 컨트롤러에서 셀 선택 상태를 추적해야합니다. 여기에서 답변 중 하나를 사용할 수 있습니다. http://stackoverflow.com/questions/28659845/swift-how-to-get-the-indexpath-row-when-a-button-in-a-cell-is-tapped/38941510 # 38941510 광산 또는 Jacob King 님이 테이블보기 컨트롤러 – Paulw11

+0

에 다시 탭 이벤트를 가져 오기 위해 @Carlo가 문제를 해결할 것입니다. –

답변

1

게시물의 정보에서,이 셀 재사용 문제처럼 보인다. 문제는 tableView이 성능을 유지하기 위해 새로운 셀을 만드는 대신 셀을 재사용한다는 것입니다. 셀의 상태를 재설정하지 않은 경우 재사용 된 셀은 이전 상태로 유지됩니다.

빠른 수정을 위해 UITableViewCellprepareForReuse 메서드를 구현할 수 있습니다.

그러나 tableView를 스크롤 한 후 확인란을 선택하려면보기 컨트롤러에서 어떤 셀을 '확인'했는지 저장해야합니다. 직접 저장할 수도 있고 tableView의 didSelectRowAtIndexPath 메소드를 사용할 수도 있습니다.

+0

나는이 문제가 세포 재사용에 관한 것이라고 생각하지만 이것을 어떻게 사용하는지 모른다. 예를 들어 대답을 편집 할 수 있는가? 제발, 나는 인터넷에서 검색하지만 나는 잘 이해하지 못한다. – Carlo

0

은 다음과 같이 그것을보십시오 :

var checkBook = UIImageView() 

if self.checkBook.image == UIImage(named: "check"){ 
    self.checkBook.image = UIImage(named: "uncheck") 
} 
else{ 
    self.checkBook.image = UIImage(named: "check") 
} 
0

있는 UITableViewCell 다시 사용할 수 있습니다. 셀에 State of View를 저장할 수 없습니다. 데이터 소스

그것을 달성하는 가장 쉬운 방법의

func tableView(UITableView, cellForRowAt: IndexPath) 

방법 당신이해야 설치 셀 UITableViewDelegate의

func tableView(UITableView, didSelectRowAt: IndexPath) 
func tableView(UITableView, didDeselectRowAt: IndexPath) 

방법을 구현하는 것입니다

그럼 당신은 추가 할 수 있습니다

/제거 이러한 메소드와 cellForRowAtIndexPath 설정 셀의 일부 배열에 대한 indexPath.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     let cell = tableView.dequeueReusableCellWithIdentifier("YourTableViewCell") as! YourTableViewCell 

     if array.contains(indexPath) { 
     cell.checkBook.image = UIImage(named: "check") 
     } else { 
     cell.checkBook.image = UIImage(named: "uncheck") 
     } 

     return cell 
} 
0

전체 셀을 클릭하는 경우 사용자 지정 셀에서 setSelected 기능을 재정의 할 수 있습니다.

override func setSelected(selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 
    if selected { 
     self.checkBook.image = UIImage(named: "check") 
    } else { 
     self.checkBook.image = UIImage(named: "uncheck") 
    } 
} 
0

내 코드를 사용해보세요.여기 selectindex은 선택된 셀 인덱스를 가져 오는데 사용되며 selectedindex은 선택된 모든 셀 값을 저장하는 NSMutableArray입니다.

var selectindex : Int? 
var selectedindex : NSMutableArray = NSMutableArray() 
@IBOutlet var tableview: UITableView! 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCellWithIdentifier("LikeCell", forIndexPath: indexPath) 
    let like: UIButton = (cell.viewWithTag(2) as! UIButton)// like button 
    let comment: UIButton = (cell.viewWithTag(3) as! UIButton) // comment button 
    comment.setBackgroundImage(UIImage(named: "chat.png"), forState: UIControlState.Normal) // comment button set 
    like.addTarget(self, action: #selector(self.CloseMethod(_:event:)), forControlEvents: .TouchDown) 
    comment.addTarget(self, action: #selector(self.CloseMethod1(_:event:)), forControlEvents: .TouchDown) 

    return cell 
} 


// This is my like button action method. 
@IBAction func CloseMethod(sender: UIButton, event: AnyObject) { 

     let touches = event.allTouches()! 
     let touch = touches.first! 
     let currentTouchPosition = touch.locationInView(self.tableview) 
     let indexPath = self.tableview.indexPathForRowAtPoint(currentTouchPosition)! 
     selectindex = indexPath.row 
     if selectedindex.containsObject(selectindex!) { 
      sender.setBackgroundImage(UIImage.init(named: "like (1).png"), forState: .Normal) 
      selectedindex.removeObject(selectindex!) 
     }else{ 
      sender.setBackgroundImage(UIImage.init(named: "like.png"), forState: .Normal) 
      selectedindex.addObject(selectindex!) 
     } 

}