【发布时间】:2018-07-25 19:49:01
【问题描述】:
我在 TableView 中有一个 UILabel ,并且该 UILabel 附加到一个 GestureRecognizer 。我想要做的是在标签点击时将 UILabel 的颜色更改为红色。现在,当点击 UILablel 时,我非常接近标签变为红色,但是错误的 TableCell 正在改变颜色。如果我点击单元格 3,那么我希望单元格 3 中的 UILabel 改变颜色,而不是单元格 4 或 5。
class HomeProfilePlacesCell: NSObject {
var CellCopy: HomeTVC?
@objc func PostTap(_ sender: UIGestureRecognizer) {
// This works but it often changes wrong cell
CellCopy?.post.textColor = UIColor.red
}
func HomeProfilePlaceTVC(_ tableView: UITableView, cellForRowAt indexPath: IndexPath, streamsModel : streamModel,HOMEPROFILE: HomeProfile, controller: UIViewController) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTVC", for: indexPath) as! HomeTVC
CellCopy = cell
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(PostTap(_:)))
tapGesture.delegate = self as? UIGestureRecognizerDelegate
cell.post.addGestureRecognizer(tapGesture)
cell.post.text = streamsModel.Posts[indexPath.row]
cell.post.tag = indexPath.row
return cell
}
}
代码再次正常工作我只需要找到一种方法将单击的单元格的索引路径到 PostTap 方法,这样它就可以更改所点击的正确单元格的颜色。任何帮助或建议都会很棒。我不想使用 Table Reload,因为我只在单击的单元格中更改了 1 个 UILabel。
【问题讨论】:
-
尝试保留对单元格 (
CellCopy) 的引用总是一个坏主意。不要这样做。 -
您一次只需要更改一种标签颜色,还是需要更改每个标签标签颜色?
标签: ios swift uitableview swift3 tableview