2014-12-01 42 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