2017-01-23 57 views
0

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

//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 

} 

回答

2

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

只需对您的状况检查稍作修改即可修复它。

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

谢谢你的工作完美:) – pete800

相关问题