使用Swift将UIButton添加到UITableViewCell的中心
以下代码仅显示表格最后一行中的按钮。有什么想法吗 而不是使用Swift将UIButton添加到UITableViewCell的中心,uitableview,swift,Uitableview,Swift,以下代码仅显示表格最后一行中的按钮。有什么想法吗 而不是 button.center = self.view.center 试一试 这假设您的tableView为全宽,cellHeight为44.0。您还无法从单元格本身获取实际高度。如果需要实际高度,则必须从tableView:willDisplayCell:forrowatinexpath:method或执行其他操作获取 另外,您不应该使用self.myTable,而应该在方法本身中使用tableView变量。Jawwad的建议修复了它。对
button.center = self.view.center
试一试
这假设您的tableView为全宽,cellHeight为44.0。您还无法从单元格本身获取实际高度。如果需要实际高度,则必须从tableView:willDisplayCell:forrowatinexpath:method或执行其他操作获取
另外,您不应该使用self.myTable,而应该在方法本身中使用tableView变量。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;
}
如果有人正在寻找上述答案的Swift 3转换,请尝试以下方法:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: textCellIdentifier, for: indexPath)
let button : UIButton = UIButton(type:UIButtonType.custom) as UIButton
button.frame = CGRect(origin: CGPoint(x: 40,y :60), size: CGSize(width: 100, height: 24))
let cellHeight: CGFloat = 44.0
button.center = CGPoint(x: view.bounds.width / 2.0, y: cellHeight / 2.0)
button.backgroundColor = UIColor.red
button.addTarget(self, action: #selector(buttonClicked), for: UIControlEvents.touchUpInside)
button.setTitle("Click Me !", for: UIControlState.normal)
cell.addSubview(button)
return cell
}
func buttonClicked(sender : UIButton!) {
print("Clicked!")
}
你在返回牢房前有一个杂散的支架。这不应该导致这个问题。也许按钮中心应该参照单元格而不是self.view?
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;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: textCellIdentifier, for: indexPath)
let button : UIButton = UIButton(type:UIButtonType.custom) as UIButton
button.frame = CGRect(origin: CGPoint(x: 40,y :60), size: CGSize(width: 100, height: 24))
let cellHeight: CGFloat = 44.0
button.center = CGPoint(x: view.bounds.width / 2.0, y: cellHeight / 2.0)
button.backgroundColor = UIColor.red
button.addTarget(self, action: #selector(buttonClicked), for: UIControlEvents.touchUpInside)
button.setTitle("Click Me !", for: UIControlState.normal)
cell.addSubview(button)
return cell
}
func buttonClicked(sender : UIButton!) {
print("Clicked!")
}