【发布时间】:2016-12-14 18:46:58
【问题描述】:
我有一个 TableView 和一个名为 Post 的标签。我有一个 Gesture Tap 功能,当用户点击该标签时,被点击的 TableView 标签会改变颜色。问题在于 tableView 回收如果我走下 tableView 然后其他未点击的单元格也会突出显示。如何修复它以便仅突出显示单击的单元格?这是我的代码
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "UserProfileTVC", for: indexPath) as! UserProfileTVC
cell.post.text = Posts[indexPath.row]
let post_tap = UITapGestureRecognizer(target: self, action: #selector(UserProfileC.post_tapped(sender:)))
cell.post.addGestureRecognizer(post_tap)
return cell
}
func post_tapped(sender:UITapGestureRecognizer) {
let point = CGPoint(x: 0, y: 0)
let position: CGPoint = sender.view!.convert(point, to: self.TableSource)
if self.TableSource.indexPathForRow(at: position) != nil {
sender.view?.backgroundColor = UIColor.blue
}
}
代码再次起作用,它突出显示了正确的 TableCell 标签,问题是当向下滚动 tableView 时,其他 tableCells 标签也被突出显示而没有被点击。
我已经用下面给出的示例更新了代码,但它仍然给出了相同的结果
if let existingRecognizerView = cell.viewWithTag(101) as UIView? {
existingRecognizerView.backgroundColor = UIColor.white
} else {
let post_tap = UITapGestureRecognizer(target: self, action: #selector(UserProfileC.post_tapped(sender:)))
post_tap.view?.tag = 101
cell.post.addGestureRecognizer(post_tap)
}
【问题讨论】:
标签: ios swift uitableview swift3