1

我有一个隐藏了字幕的 UITableView,但设置了当有人选择一个单元格时它显示该单元格的字幕的位置。这很好用,只是在点击任何单元格以显示其字幕后,如果向下滚动,您会发现每 12 个单元格的字幕未隐藏(以及应该显示的字幕)。这是我在 didSelectRowAtIndexPath 中使用的代码:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    for cell in tableView.visibleCells() {
        cell.detailTextLabel??.hidden = true
    }

    var cell = tableView.cellForRowAtIndexPath(indexPath)
    cell?.detailTextLabel?.hidden = false


}

我确信这与“.visibleCells()”有关,因为每 12 个单元格大约是我在 iPhone 6 Plus 上的可见表的高度。当我在模拟器中以 4 秒运行它时,它大约每 8 个单元格。但我不知道除了“visibleCells”还能怎么做?但这很奇怪,因为它是整个表格 - 一直向下,每 12 个单元格显示它的副标题......

谢谢你的帮助

4

2 回答 2

2

UITableView 重用它的单元格。因此,您单击的一行的单元格(取消隐藏字幕)可以用于另一行的行。解决方案是在 UITableViewCell 子类中定义 prepareForReuse() 方法(如果没有子类,则创建子类)并在那里再次隐藏字幕。

于 2015-03-07T23:15:22.877 回答
1

将该数据源的方法添加到您的控制器。应该可以正常工作。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) {
    var identifier = "cellIdentifier"
    var cell = tableView. dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath)
    cell.detailTextLabel?.hidden = true

    return cell
}
于 2015-03-07T23:35:41.313 回答