更好地使用自动布局,它将使应用程序可以在两个方向上工作。
而且在您的实现中,每次为每个单元格调用cellForRowAtIndexPath方法时,都会创建bgView,因此与仅创建bgView相比,更好地检查bgView是否为nil。请参考下面的代码。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CellID", forIndexPath: indexPath)
cell.backgroundColor = UIColor.clearColor()
if cell.backgroundView == nil {
let leftView = UIView()
let rightView = UIView()
rightView.backgroundColor = UIColor.redColor()
leftView.backgroundColor = UIColor.yellowColor()
cell.backgroundView = UIView()
cell.backgroundView?.addSubview(leftView)
cell.backgroundView?.addSubview(rightView)
leftView.translatesAutoresizingMaskIntoConstraints = false;
rightView.translatesAutoresizingMaskIntoConstraints = false;
let viewDict = ["leftView":leftView, "rightView":rightView]
cell.backgroundView!.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[leftView]-0-|", options: [], metrics: nil, views: viewDict))
cell.backgroundView!.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[rightView]-0-|", options: [], metrics: nil, views: viewDict))
cell.backgroundView!.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[leftView]-0-[rightView(==leftView)]-0-|", options: [], metrics: nil, views: viewDict))
}
cell.textLabel?.text = "dsfsdkjfh fkjhsdkjfhs kdfksdhfk sdf";
return cell
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句