在Swift中的静态TableView中向特定单元格公开指示器

时间:2017-07-07 08:44:12

标签: ios swift uitableview swift3 indexpath

我创建了一个静态TableView,我想根据我们自己的帐户或访客帐户来添加或删除披露指标。

这就是我想要的:

let index = IndexPath(row: 4, section: 0)

let cell = tableView.cellForRow(at: index)
if currentUser {
   cell.accessoryType = .none
   //cell.backgroundColor = UIColor.red
}

我试图把它放在viewDidLoad函数中,但它没有用。我也尝试了cellForRowAt indexPath和相同的结果。

我怎么能这样做?

4 个答案:

答案 0 :(得分:3)

只需检查您是否要在cellForRowAt indexPath方法中显示披露指标。

if (wantsToShow){ // Your condition goes here
   cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
}
else{
   cell.accessoryType = .none
}

那就是它。

答案 1 :(得分:1)

您正在使用静态单元格,因此cellForRow不会被调用。相反,只需拖动连接您的单元格并进行设置,就像这样

enter image description here

答案 2 :(得分:0)

请在cellForRowTableView代码中使用以下代码

它适用于你

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ 
    if currentUser {
     // Own Account
       cell.accessoryType = .none
       //cell.backgroundColor = UIColor.red
    }else{
     //Guest Account
     cell.accessoryType =.checkmark
    }
}

答案 3 :(得分:-1)

要在特定的静态单元格上添加附件,我使用了tableView,cellForRowAt,但无法访问对UITableViewCell的引用。

然后我发现了super.tableView(tableView,cellForRowAt:indexPath)

这是我的代码: 假设您知道所需的特定索引路径:

    var indexPathSort = IndexPath(item: 0, section: 0)
    var indexPathPrice = IndexPath(item: 0, section: 1)


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = super.tableView(tableView, cellForRowAt: indexPath)
            if indexPath == indexPathSort || indexPath == indexPathPrice {

                cell.accessoryType = .checkmark
            }
            return cell
        }
相关问题