以下代码仅显示表格最后一行中的按钮。有什么想法吗?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.myTable.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
let button : UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
button.frame = CGRectMake(40, 60, 100, 24)
button.center = self.view.center
button.backgroundColor = UIColor.redColor()
button.addTarget(self, action: "buttonClicked:", forControlEvents:
UIControlEvents.TouchUpInside)
button.setTitle("Click Me !", forState: UIControlState.Normal)
cell.addSubview(button
}
return cell
}
最佳答案
Jawwad 的建议解决了这个问题。这里的文档是整个工作功能:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
let button : UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
button.frame = CGRectMake(40, 60, 100, 24)
let cellHeight: CGFloat = 44.0
button.center = CGPoint(x: view.bounds.width / 2.0, y: cellHeight / 2.0)
button.backgroundColor = UIColor.redColor()
button.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
button.setTitle("Click Me !", forState: UIControlState.Normal)
cell.addSubview(button)
return cell;
}
关于uitableview - 使用 Swift 将 UIButton 添加到 UITableViewCell 的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27967338/