ios - 禁用除最后一个单元格之外的所有 UITableView 单元格 - Swift

标签 ios arrays swift uitableview cell

目前正在 swift 中使用 UITableView。

由于我的表格单元格值是通过单击按钮附加的,因此我想找出一种方法来禁用除最后一个单元格之外的所有先前单元格的交互。

我尝试过一些基于数组的方法,但它并没有达到我想要的效果。

var disabledRows = [0,1,2]
        else
    {
        cell.backgroundColor = UIColor.orangeColor()
    }

    if (contains(disabledRows, indexPath.row)) {
        cell.userInteractionEnabled = false
    }
    else {
        cell.userInteractionEnabled = true
    }

这种方法的问题是我必须计算所有单元格,然后阻止最后一个单元格。

也许有一些更简单的方法?

非常感谢任何帮助。

更新 - 这是我完整的“cellForRowAtIndexPath”函数

func tableView(tableView: UITableView, cellForRowAtIndexPath
    indexPath: NSIndexPath) -> UITableViewCell
{
    let cell: CustomCellForTableViewTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomCellForTableViewTableViewCell

    if indexPath.row % 2 == 0
    {
        cell.backgroundColor = UIColor.purpleColor()
    }
    else
    {
        cell.backgroundColor = UIColor.orangeColor()
    }

    if (contains(disabledRows, indexPath.row)) {
        cell.userInteractionEnabled = false
    }
    else {
        cell.userInteractionEnabled = true
    }

    let quest = arrayOfQuestions[indexPath.row]
    cell.setCell(type.Grocery, optionno1:type.option1, optionno2:type.option2)

    cell.mainText.text = type.Grocery


    cell.optionOne.backgroundColor = UIColor.redColor()

    return cell

}

最佳答案

在 cellForRowAtIndexPath: 方法中。

if (indexPath.row == yourDataSourceArray.count - 1) {
// enable cell
} else {
// disable cell
}

关于ios - 禁用除最后一个单元格之外的所有 UITableView 单元格 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31967403/

相关文章:

c++ - 列出 Xcode、iOS 中最后的 X malloc/free 调用

ios - 无法弄清楚为什么 Xcode 模拟器无法正常工作

java - 我如何获得数组来保存所玩游戏的数量并为每个玩家运行它?

c++ - 数组的值随机变化

swift - 我如何在 Swift 中将 "6 September, 2015"转换为 NSDate()?

iOS 结果类型 'Element' 与预期类型不匹配(Swift - Coredata)

ios - 如何在iOS中的核心数据中添加json解析数据?

c# - 遍历数组

ios - 如何从字符串数组创建枚举值数组

ios - 为什么从另一个具有不同颜色的 viewController 返回后导航栏颜色会发生变化