Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在UITableViewCell中更改附件视图的背景_Uitableview_Swift - Fatal编程技术网

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

在UITableViewCell中更改附件视图的背景,uitableview,swift,Uitableview,Swift,根据UITableViewCell,它由两部分组成: 我为附件视图设置了自定义图像: func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("settingCell") as! SettingsTableViewCe

根据UITableViewCell,它由两部分组成:

我为附件视图设置了自定义图像:

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, 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

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


您必须使用
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

}

当您自己更改所选单元格的背景时,可以将选择样式设置为“无”,而不是灰色或蓝色。
selectedCell.selectionStyle=UITableViewCellSelectionStyle.None
应在
selectedCell.contentView.superview?.backgroundColor=UIColor.orangeColor()之前设置
我猜selectedCell.setDeceledColor()这没有用吗?