创建cell
let cell=tableView .dequeueReusableCellWithIdentifier(cellIdenty, forIndexPath: indexPath) as! UITableViewCell
但是在点击cell中的按钮时,重复利用的cell里面按钮都会响应,这个问题应该怎么解决呢,请大神们指点啊
在Swift里面怎么避免cell的重复利用呢?
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
你知我知皆知 2024-08-04 23:30关注以下回答参考 皆我百晓生、券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。
你可以使用
reuseIdentifier属性来避免重复利用。当你在同一个视图中添加多个Cell时,你应该为每个Cell分配一个唯一的reuse identifier。let reuseIdentifier = "MyCell" var myCell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! MyTableViewCell然后在你的Cell类中设置你的button:
@IBAction func buttonTapped(_ sender: UIButton) { // Your code here }这样,当用户点击按钮时,你只需要更新按钮的文本,而不需要重新创建Cell。
解决 无用评论 打赏 举报