ios - 当我滚动我的 tableview 时,事件的 tableView 单元格不断被禁用

标签 ios swift uitableview swift2 cell

我的应用有一个包含不同部分的 UITableView。我只想允许访问前 3 个部分,即索引路径 0、1 和 2。我的问题是我的代码在应用程序启动时有效。但是,当我向下滚动表格 View 部分并向上滚动表格 View 部分的顶部时,当我返回到它们时,第 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

}

最佳答案

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

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

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

关于ios - 当我滚动我的 tableview 时,事件的 tableView 单元格不断被禁用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41811819/

相关文章:

swift - 您能否将类型和协议(protocol)结合起来,并将其用作枚举的基本类型?

ios - Swift 3 - NSCoding 没有从 NSObject 继承的烦恼

ios - 自定义 XIB UITableViewCell 显示不正确

objective-c - PromiseKit 框架 - 对成员 'then()' 的模糊引用

swift - 响应 UITableView 等手势的 UIStackView

iphone - 从 Apple DateCell 模板中删除单元格

ios - 关闭 UIViewController 时重新加载 TableView?

ios - 我的 iOS 程序将自己宣传为什么 HTTP 用户代理?

ios - 在 CloudKit 中处理多对多关系

ios - 如何为 'animated: true' 添加完成处理程序/关闭