我有自定义的tableview。
class CustomCell: UITableViewCell {
var firstBtn : UIButton!
var secondBtn : UIButton!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
// button frame coding
self.contentView.addSubview(firstBtn)
self.contentView.addSubview(secondBtn)
}
在视图控制器中,我以编程方式创建了tableview。
colorTableView.frame = CGRectMake(20,50,100, 200)
colorTableView.delegate = self
colorTableView.dataSource = self
colorView.addSubview(colorTableView)
问题是选择一个突出显示为灰色的单元格。但是当我按下同一单元格中的按钮时,突出显示颜色消失。所以我无法找到选择的单元格。
我需要继续选择细胞直到下一个细胞。
我尝试通过手动给出背景颜色来修复它,但是这不能按照我的意愿工作。
Cell?.contentView.backgroundColor = UIColor.lightGrayColor()
Cell?.textLabel?.backgroundColor = UIColor.clearColor()
Cellforindexpath
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell
cell.contentView.addSubview(cell.firstbutton)
cell.firstbutton.addTarget(self, action: "buttonpress:", forControlEvents: UIControlEvents.TouchUpInside)
}
答案 0 :(得分:1)
这是一个解决方案。
创建一个类似 - var currentSelectedIndex: NSIndexPath!
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
if self.currentSelectedIndex != indexPath {
let previosSelectedCell = tableView.cellForRowAtIndexPath(self.currentSelectedIndex) as! UITableViewCell
selectedCell.backgroundColor = UIColor.clearColor()
self.currentSelectedIndex = indexPath
let selectedCell = tableView.cellForRowAtIndexPath(indexPath) as! UITableViewCell
selectedCell.backgroundColor = UIColor.redColor()
}
}
答案 1 :(得分:0)
为您的按钮编写自定义功能,例如(使用按钮作为发件人)
您的按钮功能:
func buttonpress(sender: UIButton) {
// get the cell and table
var cell: UITableViewCell = sender.superview.superview as UITableViewCell
var tableView: UITableView = cell.superview as UITableView
let rowToSelectIndexPath = tableView.indexPathForCell(cell)
tableView.selectRowAtIndexPath(rowToSelectIndexPath, animated: true, scrollPosition:UITableViewScrollPosition.None)
}
答案 2 :(得分:0)
我认为您不应该依赖选定的行索引,只需使用按钮的tag属性找出特定的发件人。
答案 3 :(得分:0)
上帝格蕾丝终于得到了解决方案。
第一步:存储选定行的索引路径
func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
println("row selected")
GetIndexOfRow = indexPath
return GetIndexOfRow
}
然后在cellForAtIndexPath方法中:
if getIndexpathOfRow != nil {
if indexPath == getIndexpathOfRow {
cell.contentView.backgroundColor = UIColor(white: 0.85, alpha: 1)
cell.textLabel?.backgroundColor = UIColor(white: 0.85, alpha: 1)
}else {
cell.contentView.backgroundColor = UIColor.whiteColor()
cell.textLabel?.backgroundColor = UIColor.whiteColor()
}
}
这些选择的行以浅灰色作为tableview单元格的默认选择颜色。
for view in self.view.subviews {
for each in view.subviews {
if each.isKindOfClass(UITableView){
var getTableview = each as! UITableView
for (var j = 0; j < getTableview.numberOfSections(); ++j)
{
for (var i = 0; i < getTableview.numberOfRowsInSection(j); ++i)
{
var cells = getTableview.cellForRowAtIndexPath(NSIndexPath(forRow: i, inSection: j))
if cells?.textLabel?.backgroundColor == UIColor(white: 0.85, alpha: 1.0) {
cells?.contentView.backgroundColor = UIColor.whiteColor()
cells?.textLabel?.backgroundColor = UIColor.whiteColor()
}
}
}
}
}
}
上面的代码如下工作。当我们选择一行时,它检查可见单元格背景单元格颜色。如果任何其他单元格有颜色均值。更改为清除颜色(在我的情况下为白色)。所以我们只能看到选定的单元格高亮。
即使在单元格中使用外部按钮或按钮,这也足以使单元格突出显示。
希望它有所帮助。