【问题标题】:Active tableView cells keep getting disabled when I scroll through my tableview当我滚动浏览我的表格视图时,活动的表格视图单元格不断被禁用
【发布时间】:2017-01-23 17:09:14
【问题描述】:

我有一个包含不同部分的 UITableView 的应用程序。我只想允许访问前 3 个部分,即索引路径 0、1 和 2。我的问题是我的代码在应用程序启动时有效。但是,当我向下滚动表格视图部分并向上滚动表格视图部分的顶部时,当我回到它们时,0、1 和 2 部分被禁用。我怎样才能解决这个问题?

//formatting the cells that display the sections
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell!

    cell.textLabel?.text = sectionName[indexPath.row]
    cell.textLabel?.textAlignment = .Center
    cell.textLabel?.font = UIFont(name: "Avenir", size:30)

    //Code to block disable every section after row 3.
    if ( indexPath.row  >= 2 ) {
    cell.userInteractionEnabled = false
    cell.contentView.alpha = 0.5
    }

    return cell

}

【问题讨论】:

    标签: ios swift uitableview swift2 cell


    【解决方案1】:

    细胞正在被重复使用。单元格被重复使用并且不会再次创建以提高性能。因此,当您向下滚动时,由于您的条件检查,单元格的交互将被禁用。由于没有条件检查indexPath.row 是否低于 2,因此与重用单元格的用户交互保持不变(false)。

    只需对您的条件检查稍作修改即可解决问题。

    if ( indexPath.row  >= 2 ) {
        cell.userInteractionEnabled = false
        cell.contentView.alpha = 0.5
    }
    else{
        cell.userInteractionEnabled = true
        cell.contentView.alpha = 1
    }
    

    【讨论】:

      猜你喜欢
      • 2013-12-30
      • 2019-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多