2014-12-01 84 views
1

我想在应用约束后在我的container1的中间应用一行问题是中间行在每行中应用两次。似乎layOutSubviews方法正在为每行执行两次,我很困惑为什么。?约束在每个单元上之前和之后被执行在我的自定义细胞TableViewCell被调用两次

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

layOutSubviews方法?

的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! 





} 

回答

2

layoutSubviews实现需要是idempotent。每当视图的frame更改时都可以调用它。您不应该在此方法中添加子视图。相反,在init方法中添加子视图并将其框架设置为layoutSubviews