2

我创建了一个静态 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

5 回答 5

3

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

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

而已。

于 2017-07-07T09:02:31.500 回答
2

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

在此处输入图像描述

于 2017-07-07T13:30:34.927 回答
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
    }
}
于 2017-07-07T09:06:58.610 回答
0

迅速

特定细胞

if indexPath.row == 1 {
    cell.accessoryType = .disclosureIndicator
} else {
    cell.accessoryType = .none
}
于 2021-11-26T11:04:05.263 回答
-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
        }
于 2019-09-13T00:04:45.327 回答