gpt4 book ai didi

uitableview - 在 UITableViewCell 中更改附件 View 的背景

转载 作者:可可西里 更新时间:2023-11-01 01:40:01 25 4
gpt4 key购买 nike

根据 Apple docs UITableViewCell 由两部分组成: enter image description here

我为辅助 View 设置了自定义图像:

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

}

但结果不是我想要的,Accessory View 仍然是灰色背景,但我希望它是橙色的:

enter image description here

最佳答案

您必须使用 UITableViewCellSelectionStyle.None 将取消选择的单元格 selectionStyle 设置为 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

}

关于uitableview - 在 UITableViewCell 中更改附件 View 的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30709988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com