如何更改 tableViewCellForRowAtIndex 中 customTableViewCell 中的按钮框架

How to change button frame in customTableViewCell in tableViewCellForRowAtIndex

我想让我的按钮更宽一点,这是我正在尝试使用的代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("secondItemTableCell") as! SecondItemTableViewCell
    cell.docentBtn.setTitle(data[subjects[indexPath.section]]![indexPath.row], forState: UIControlState.Normal)
    println(cell.docentBtn.frame)
    cell.docentBtn.frame = CGRectMake(cell.docentBtn.frame.origin.x, cell.docentBtn.frame.origin.y, cell.docentBtn.frame.width + 20.0, cell.docentBtn.frame.height)
    cell.docentBtn.layer.cornerRadius = cell.docentBtn.frame.height / 2
    cell.docentBtn.clipsToBounds = true
    println(cell.docentBtn.frame)
    return cell
}

输出:

(8.0, 5.0, 49.0, 34.0)
(8.0, 5.0, 69.0, 34.0)

但视觉上没有任何变化。谁能帮我看看哪里出了问题?

通过向我的自定义单元格添加按钮宽度约束来解决 class,然后在 cellForRowAtIndex 中更新它的值。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("secondItemTableCell") as! SecondItemTableViewCell
    cell.docentBtn.setTitle(data[subjects[indexPath.section]]![indexPath.row], forState: UIControlState.Normal)
    cell.docentBtnWidth.constant = cell.docentBtn.frame.width + 25.0
    cell.docentBtn.layer.cornerRadius = cell.docentBtn.frame.height / 2
    cell.docentBtn.clipsToBounds = true
    return cell
}