Brosef Brosef - 5 months ago 18
iOS Question

button in custom tableview cell not displaying

I have a

UITableView
and each cell is associated with a file. The tableview shows files from all users and when the file for the cell belongs to the current user, the cell displays a edit button. My
cellForRowAtIndexPath
will check the userID associated with the file and compare it with the current users ID. If the ID's match, the button is not hidden.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{

let cell = self.fileTable.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! FileCell
let file = self.tableFiles[indexPath.row]
cell.fileLabel.text = file.title
cell.userLabel.text = file.username

if file.userId != user?.userID
{
cell.editButton.hidden = true
}

cell.editButton.addTarget(self, action:#selector(HomeController.editPressed(_:)), forControlEvents: UIControlEvents.TouchUpInside)

return cell
}


This works fine when a user logs in and the tableview is loaded for the first time after login. But when a user creates a new file to be added to the tableview
self.navigationController?.popViewControllerAnimated(true)
is called and the tableview reappears. However, the edit button for the new file isn't being shown on the new file or any future added files unless the user logs out and logs back in.

Answer

Following should solve you problem.

if file.userId != user?.userID
{
    cell.editButton.hidden = true
}
else
{
    cell.editButton.hidden = false

}
Comments