在UITableViewCell中更改附件视图的背景

时间:2015-06-08 13:07:17

标签: uitableview swift

根据Apple docs UITableViewCell包含两个部分: enter image description here

我为附件视图

设置了自定义图像
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("settingCell") as! SettingsTableViewCell
    cell.setCellColor(self.items[indexPath.row])
    let img = UIImage(named: "icon-right-arrow")!
    let imgView:UIImageView = UIImageView()
    imgView.contentMode = UIViewContentMode.ScaleAspectFill
    imgView.frame.size.width = img.size.width
    imgView.frame.size.height = img.size.height
    imgView.image = img
    imgView.image = imgView.image!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
    imgView.tintColor = UIColor.orangeColor()
    cell.accessoryView = imgView

    return cell
}

单元格选择时触发的功能

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let selectedCell = tableView.cellForRowAtIndexPath(indexPath) as! SettingsTableViewCell
    selectedCell.setSelectedColor()
    let img = UIImage(named: "icon-right-arrow")!
    let imgView:UIImageView = UIImageView()
    imgView.contentMode = UIViewContentMode.Center
    imgView.frame.size.width = selectedCell.accessoryView!.frame.width
    imgView.frame.size.height = selectedCell.accessoryView!.frame.height
    imgView.backgroundColor = UIColor.orangeColor()
    imgView.image = img
    imgView.image = imgView.image!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
    imgView.tintColor = UIColor.whiteColor()
    selectedCell.accessoryView = imgView

    selectedCell.contentView.backgroundColor = UIColor.orangeColor()

}

取消选择单元格:

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    let selectedCell = tableView.cellForRowAtIndexPath(indexPath) as! SettingsTableViewCell
    selectedCell.setDeSelectedColor()
    let img = UIImage(named: "icon-right-arrow")!
    let imgView:UIImageView = UIImageView()
    imgView.contentMode = UIViewContentMode.Center
    imgView.frame.size.width = selectedCell.accessoryView!.frame.width
    imgView.frame.size.height = selectedCell.accessoryView!.frame.height
    imgView.backgroundColor = UIColor.clearColor()
    imgView.image = img
    imgView.image = imgView.image!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
    imgView.tintColor = UIColor.orangeColor()
    selectedCell.accessoryView = imgView

}

但结果不是我想要的,附件视图仍然有灰色背景,但我希望它是橙色的:

enter image description here

1 个答案:

答案 0 :(得分:2)

您必须使用UITableViewCellSelectionStyle.None

将取消选择的单元格选择样式设置为无
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    let selectedCell = tableView.cellForRowAtIndexPath(indexPath) as! SettingsTableViewCell
    selectedCell.selectionStyle = UITableViewCellSelectionStyle.None
    selectedCell.setDeSelectedColor()
    let img = UIImage(named: "icon-right-arrow")!
    let imgView:UIImageView = UIImageView()
    imgView.contentMode = UIViewContentMode.Center
    imgView.frame.size.width = selectedCell.accessoryView!.frame.width
    imgView.frame.size.height = selectedCell.accessoryView!.frame.height
    imgView.backgroundColor = UIColor.clearColor()
    imgView.image = img
    imgView.image = imgView.image!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
    imgView.tintColor = UIColor.orangeColor()
    selectedCell.accessoryView = imgView

}