从SWIFT中的UITableViewCell中删除子视图

时间:2015-07-09 18:58:18

标签: swift uitableview subview

我在UITableViewCell中有3个UILabel,当我尝试更新单元格并且没有正确更新时,它会导致我出现问题。我希望能够从单元格中删除所有子视图,然后创建新的UILabel,然后这应该工作。如何从单元格中删除所有子视图?

对不起,如果有一个广泛的问题,但不知道从哪里开始。

4 个答案:

答案 0 :(得分:6)

你在找这样的东西吗?

    for view in yourTableViewCell?.subviews  as! [UIView]{
        if let label = view as? UILabel {
            label.removeFromSuperview()
        }
    }

答案 1 :(得分:0)

如果您将UITableViewCell子类化,那么您可以将插件(在自定义UITableViewCell类中)添加到其中的子视图中。当调用下面的方法时,您可以更新它的标签并使用它的条件:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell:CustomUITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as CustomUITableViewCell

    cell.textLabel?.text = self.items[indexPath.row]

    return cell
}  

答案 2 :(得分:0)

您转到File-> New->文件,选择iOS-> Source-> cocoatouch类,并在类中:CustomTableViewCell,子类:UITableViewCell(从下拉菜单中选择)。

https://www.dropbox.com/s/17mzkrtgnwdophq/Screen%20Shot%202015-07-09%20at%204.06.50%20PM.png?dl=0

创建自定义单元格类后:

在故事板中选择tableview中的原型单元格,在身份检查器中,您必须将其类设置为CustomUITableViewCell

https://www.dropbox.com/home?preview=Screen+Shot+2015-07-09+at+4.07.04+PM.png

你添加这样的插座,你可以参考,就像我在上一个答案中所做的那样!

class CustomTableViewCell : UITableViewCell {
    @IBOutlet var backgroundImage: UIImageView
    @IBOutlet var titleLabel: UILabel
}

希望这有帮助!

答案 3 :(得分:0)

这是swift的快速解决方案:

b_mat
相关问题