【问题标题】:How to change button frame in customTableViewCell in tableViewCellForRowAtIndex如何更改 tableViewCellForRowAtIndex 中 customTableViewCell 中的按钮框架
【发布时间】:2015-08-27 20:46:20
【问题描述】:

我想让我的按钮更宽一些,下面是我正在尝试的代码:

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)

但视觉上没有任何变化。有人可以帮我看看有什么问题吗?

【问题讨论】:

标签: ios swift uitableview uibutton frame


【解决方案1】:

通过将按钮宽度约束添加到我的自定义单元格类,然后在 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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 2011-02-09
    相关资源
    最近更新 更多