Change editing control image for UITableViewCell

纵饮孤独 提交于 2021-01-29 05:28:02

问题


Is it possible to change the editing control image for multiple row selection for UITableViewCell. If it is possible via any default function of UITableViewCell or we have to customize it on our own, if so could anyone give me any reference to do it.

Thanks in advance.


回答1:


The OP is actually wanting to change the editing control. That is the plus or minus that appears to the left of the cell when you change it to editing mode. You can't actually change the image, but what you can do is fake changing the image, here are the steps:

  1. Create a UITableViewCell subclass.
  2. Override initWithCoder:. In that method, add a UIView to self.backgroundView.
  3. Override willTransitionToState:. You do not need to actually add anything here, but adding this method override prevents the cell from adding the system editing controls.
  4. Add the image that you want as the editing control as a sub view to self.backgroundView.

Now, when the table enters editing mode and the cells shift the content to the right, you will see the image that you added to the background view. The side effect of this is that it will not recieve the touch when you touch your new image. For my case I set tableView.allowSelectionDuringEditing to YES and handled selection in tableView:didSelectRowAtIndexPath: in my table view controller.




回答2:


UITableViewCell's accessoryType property has a possible value of UITableViewCellAccessoryCheckmark which will display a checkmark on the right hand side of the cell, useful for noting selected/deselected status.

The other options are UITableViewCellAccessoryDisclosureIndicator which is a chevron pointing right, and UITableViewCellAccessoryDetailDisclosureButton which makes the little blue button like in the Recent Call Log of the Phone app.

So after setting up your cell,

cell.textLabel.text = @"Selected";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
return cell;



回答3:


Overwriting the custom cells setEditing should work. Works for me. However I am still unable to figure out, why it goes back to the default image when reordering is taking place.

Any suggestions would be welcome :)

private var myEditImage = UIImage(named: "some-image")

override func setEditing(_ editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: animated)

    for subViewA in self.subviews {
        if subViewA.classForCoder.description() == "UITableViewCellEditControl" {
            if let subViewB = subViewA.subviews.last {
                if subViewB.isKind(of: UIImageView.classForCoder()) {
                    let imageView = subViewB as! UIImageView;
                    imageView.contentMode = .scaleAspectFit
                    imageView.image = self.myEditImage;
                }
            }
        }
    }
}


来源:https://stackoverflow.com/questions/10396182/change-editing-control-image-for-uitableviewcell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!