简体   繁体   中英

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.

Following should solve you problem.

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM