我想在下面的代码中使段标题有条件,这样我就可以添加" +"一个部分标题上的按钮。以下是我的方法:
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let customSecHeader = UIView()
if (section == 0)
{
let titleLabel = UILabel()
let addButton = UIButton()
titleLabel.text = "\(self.section[section]) (\(self.items[section].count))"
titleLabel.font = UIFont(name: "MarkerFelt-Thin", size: 15)
titleLabel.textColor = UIColor.redColor()
titleLabel.textAlignment = .Left
titleLabel.frame = CGRectMake(5,5,140,30)
addButton.setTitle("+", forState: .Normal)
addButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
addButton.frame = CGRectMake(60,5,10,10)
customSecHeader.addSubview(titleLabel)
customSecHeader.addSubview(addButton)
}
else
{
let titleLabel = UILabel()
titleLabel.text = "\(self.section[section]) (\(self.items[section].count))"
titleLabel.font = UIFont(name: "MarkerFelt-Thin", size: 15)
titleLabel.textColor = UIColor.redColor()
titleLabel.textAlignment = .Center
titleLabel.frame = CGRectMake(5,5,140,30)
customSecHeader.addSubview(titleLabel)
}
return customSecHeader
}
但是这段代码不起作用。当我尝试调试这个方法时,我发现这个方法在section = 1时执行了一次,但我的表中有2个部分。