简体   繁体   English

自定义表格视图单元格中的按钮未显示

[英]button in custom tableview cell not displaying

I have a UITableView and each cell is associated with a file. 我有一个UITableView ,每个单元都与一个文件关联。 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. 我的cellForRowAtIndexPath将检查与文件关联的userID并将其与当前用户ID进行比较。 If the ID's match, the button is not hidden. 如果ID匹配,则按钮不会隐藏。

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. 但是,当用户创建要添加到表self.navigationController?.popViewControllerAnimated(true)的新文件时,将self.navigationController?.popViewControllerAnimated(true)并重新出现该表视图。 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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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