2

I created a static TableView and I would like to add or remove a disclosure indicator depending if we are consulting our own account or a guest account.

This is what I would like :

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

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

I tried to put it in the viewDidLoad function but it didn't work. I tried cellForRowAt indexPath also and same result.

How could I do that?

halfer
  • 19,824
  • 17
  • 99
  • 186
KevinB
  • 2,454
  • 3
  • 25
  • 49

5 Answers5

3

Just check if you want to show disclosure indicator in cellForRowAt indexPath method.

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

That's it.

Niraj
  • 1,939
  • 20
  • 29
  • The problem is that it's a static TableView and I would like do this only on the cell [0, 4] ... – KevinB Jul 07 '17 at 11:43
2

Your are working with static cells so cellForRow will not get called. Instead, simply drag connect your cell and set it up, like this

enter image description here

Fangming
  • 24,551
  • 6
  • 100
  • 90
0

Please use below code in cellForRowTableView code

It will work for you

 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
    }
}
Aman.Samghani
  • 2,151
  • 3
  • 12
  • 27
0

Swift

Specific cell

if indexPath.row == 1 {
    cell.accessoryType = .disclosureIndicator
} else {
    cell.accessoryType = .none
}
Akbar Khan
  • 2,215
  • 19
  • 27
-1

To add accessory on a specific static cell, I used tableView, cellForRowAt but i couldn't access a reference to UITableViewCell.

Then i found super.tableView(tableView, cellForRowAt: indexPath)

So here is my code: Assuming you know the specific indexpath you want:

    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
        }
JonesJr876
  • 39
  • 2