UITableViewCell被调用两次

时间:2014-12-01 19:58:33

标签: ios iphone uitableview swift

我正在尝试在应用约束后在container1的中间应用一行。问题是中间行在每一行中应用了两次。似乎layOutSubviews方法每行执行两次,我很困惑为什么。?

我的自定义单元格中的layOutSubviews方法

override func layoutSubviews() {
    super.layoutSubviews()
    println(self.container1?.frame.height)

    var middleLine1:UIView = UIView(frame: CGRectMake(self.container1!.frame.origin.x, self.container1!.frame.origin.y+(self.container1!.frame.height/2), 10, 1))
    middleLine1.backgroundColor = UIColor(rgba: "#cccac8")
    self.addSubview(middleLine1)



}

println是在约束之前执行还是在每个Cell之后执行?

tableView委托Methdos

func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int)  -> Int {



    return 1

}


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



    var cell  = tableView.dequeueReusableCellWithIdentifier("BracketCell") as? BracketCell

    cell?.selectionStyle = UITableViewCellSelectionStyle.None





    return cell!





}

1 个答案:

答案 0 :(得分:2)

您的layoutSubviews实施必须是idempotent。每次查看frame更改时都可以调用它。您不应该在此方法中添加子视图。而是在init方法中添加子视图,并将其框架设置为layoutSubviews