【发布时间】:2016-02-27 04:44:02
【问题描述】:
当我点击自定义单元格中的按钮然后向下(或向上)滚动时,也会点击另一个单元格按钮。我看到它被点击了,因为我为按钮创建的按钮插座被禁用了。
我的cellForRowAtIndexPath 有一个用于单元格的重用标识符:
var cell: FeedTableViewCell? = tableView.dequeueReusableCellWithIdentifier("MusicCell") as? FeedTableViewCell
考虑到我在cellForRowAtIndexPath 中有degueueReusableCellWithId,我需要prepareForReuse 吗?当我在我的自定义单元格文件中添加prepareForReuse 时,单元格只是回到默认值(显然是因为我将其重置为默认值)。问题是我希望它保留每个 indexPath.row 的值。
这就是我查询值的方式:
override func queryForTable() -> PFQuery {
var query:PFQuery = PFQuery(className:"Music")
if(objects?.count == 0)
{
query.cachePolicy = PFCachePolicy.CacheThenNetwork
}
query.orderByAscending("videoId")
return query
}
这是numberOfRowsInSection 和cellForRowAtIndexPath
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objects!.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
var cell: FeedTableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? FeedTableViewCell
if(cell == nil) {
cell = NSBundle.mainBundle().loadNibNamed("FeedTableViewCell", owner: self, options: nil)[0] as? FeedTableViewCell
}
if let pfObject = object {
//I took out the irrelevant methods. I can add them if that makes a difference...
var votes:Int? = pfObject["votes"] as? Int
if votes == nil {
votes = 0
}
cell?.votesLabel?.text = "\(votes!)"
}
我在 super.viewDidLoad() 上方的viewDidLoad 中注册了这个
tableView.registerNib(UINib(nibName: "FeedTableViewCell", bundle: nil), forCellReuseIdentifier: cellIdentifier)
这是我在 customCell 中的按钮查询:
@IBAction func heartButton(sender: AnyObject) {
if(parseObject != nil) {
if var votes:Int? = parseObject!.objectForKey("votes") as? Int {
votes!++
parseObject!.setObject(votes!, forKey: "votes")
parseObject!.saveInBackground()
votesLabel?.text = "\(votes!)"
}
}
heartOutlet.enabled = false
}
任何帮助和建议都意义重大。
谢谢。
我使用的参考链接:
我提到了几个链接,但它们在 Objective-c 中并没有帮助:
UICollectionView Tap Selects More Than One Cell
How to use prepareForReuse method
我还参考了文档,但这并没有太大帮助。
【问题讨论】:
-
添加UITableView数据源方法的代码
-
我在哪里从 tableView 查询和发送数据?还是来自自定义单元格? @HarikrishnanT
-
tbale查看数据源方法包括
cellForRowAtIndexPath、numberOfCellsInTableView等,请贴出cellForRowAtIndexPath的代码(如果你也在使用willDisplayCell)。一定是你做错了什么导致了问题。 -
刚刚更新了问题,谢谢@HarikrishnanT
标签: swift uitableview custom-cell prepareforreuse