【发布时间】:2015-08-10 16:52:52
【问题描述】:
还有另一个具有相同逻辑的问题。UITableViewCell subview disappears when cell is selected 我没有得到我想要的正确解决方案。他们建议像这样子类化视图。但我需要在单元格本身内显示按钮。
让我们来回答我的问题:
我有一个自定义和以编程方式创建的表格视图。
请看截图。
在此我以编程方式将按钮添加到 tableview 单元格。
让我们来解决问题。
当我选择任何单元格时,它也会隐藏按钮,我想看到那个按钮。
我的代码在这里:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
// here is the redbutton
var redBtn = UIButton()
redBtn = UIButton(frame: CGRectMake(0, 0, 40, 40))
redBtn.backgroundColor = UIColor.redColor()
cell.addSubview(redBtn)
//label text just added from an array
cell.textLabel?.textAlignment = NSTextAlignment.Center
cell.textLabel?.text = items[indexPath.row]
return cell
}
如果需要:Tableview创建代码:
var tableView: UITableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
tableView.frame = CGRectMake(0, 50, self.view.frame.width,self.view.frame.height);
tableView.delegate = self
tableView.dataSource = self
tableView.estimatedRowHeight = 30
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.view.addSubview(tableView)
}
提前致谢。
【问题讨论】:
-
您可以提供单元格选择样式无。 => cell.selectionStyle = .None
-
@HoaParis 是的,但我无法从中找到解决方案。如果您使用我的代码发布答案会很有帮助。
标签: uitableview swift selection custom-cell