[英]UITableViewAutomaticDimension doesn't work for cell that contains tableView
[英]Reusing cell doesn't work well - TableView
我對手機的按鈕有疑問。 在我的tableView中,每一行都由以下內容組成:圖像,一些標簽和一個按鈕。 該按鈕上有一個選中標記圖像。 單擊后,按鈕的圖像改變。 問題在於另一個按鈕的圖像也會無故更改。 發生此錯誤是因為我的單元被重用了。
我試圖在TableViewCell中使用prepareForReuse
方法,但沒有任何反應。 我也嘗試了selectedRowAt
但沒有任何結果。 請幫我。
圖片1
圖片2:
這是我custom Cell:
函數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"
}
}
在我的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"
}
}
從帖子中的信息來看,這看起來像是單元重用問題。 問題在於tableView
重用了單元而不是創建新的單元以保持性能。 如果您尚未重置單元的狀態,則重用的單元將保持配置為舊狀態。
為了快速修復,可以在UITableViewCell
上實現prepareForReuse
方法。
但是,如果希望在滾動tableView之后選中該復選框,則需要在視圖控制器中存儲“選中”的單元格。 您可以自己存儲它,也可以使用tableView的didSelectRowAtIndexPath
方法。
嘗試這樣做:
var checkBook = UIImageView()
if self.checkBook.image == UIImage(named: "check"){
self.checkBook.image = UIImage(named: "uncheck")
}
else{
self.checkBook.image = UIImage(named: "check")
}
UITableViewCell是可重用的。 您無法在單元格中存儲視圖狀態。 您應該在其中設置單元格
func tableView(UITableView, cellForRowAt: IndexPath)
數據源的方法
實現該目標的最簡單方法是實施
func tableView(UITableView, didSelectRowAt: IndexPath)
func tableView(UITableView, didDeselectRowAt: IndexPath)
UITableViewDelegate的方法
然后,您可以在這些方法和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
}
如果要單擊整個單元格,則可以像這樣在自定義單元格中覆蓋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")
}
}
試試我的代碼。 在這里, 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!)
}
}
我最近遇到了這個問題,但沒有發現太多。 經過大量搜索之后,解決的方法是使用:
override func prepareForReuse() {
btnAdd.setImage(nil, for: .normal) //here I use to change to none image
super.prepareForReuse()
}
只需將此方法放入自定義UITableViewCell
,然后設置要實現統計的項目即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.