在多次选择tableview时,使用自定义图像替换复选标记

时间:2016-04-09 01:50:45

标签: ios objective-c uitableview

我尝试用自定义图片替换复选标记。我使用如下方法:

-(void)layoutSubviews
{
    [super layoutSubviews];
    for (UIControl *control in self.subviews){
        if ([control isMemberOfClass:NSClassFromString(@"UITableViewCellEditControl")]){
            for (UIView *v in control.subviews)
            {
                if ([v isKindOfClass: [UIImageView class]]) {
                    UIImageView *img=(UIImageView *)v;
                    if (self.selected) {
                        img.image= [UIImage createImageWithColor:[UIColor redColor]];
                    }else
                    {
                        img.image= [UIImage createImageWithColor:[UIColor yellowColor]];
                    }
                }
            }
        }
    }
}

即使仍然存在问题,它仍能正常工作。在UIControl开头没有self.subviews

enter image description here

如图所示。在我点击之前,复选标记没有被黄色图像替换。

2 个答案:

答案 0 :(得分:1)

你应该在-layoutSubviews和-didTransitionToState中调用-setImageForCheckMark。 -didTransitionToState用于更改单元格状态,-layoutSubviews用于更改单击状态

-(void)layoutSubviews
{
    // well ,while you change your cells' state into EditState ,-layoutSubviews will be invoked.
    // but the _imageView of control always be nil at end of layoutSubviews
    // I've try KVO to observe this _imageView ,but obviously it's not an attribute
    // Thankfully, UITableViewCell provide us with -willTransitionToState/-didTransitionToState
    [super layoutSubviews];
    [self setImageForCheckMark];
}


- (void)didTransitionToState:(UITableViewCellStateMask)state{
    // you should override it by your subclass ,when this method get called
    // all details of cell will be completed
    if (UITableViewCellStateShowingEditControlMask) {
        [self setImageForCheckMark];
    }
}

- (void)setImageForCheckMark{
    for (UIControl *control in self.subviews){
        if ([control isMemberOfClass:NSClassFromString(@"UITableViewCellEditControl")]){
            UIImageView *image = [control valueForKey:@"_imageView"];
            imageView.image = [UIImage createImageWithColor:self.selected?[UIColor yellowColor]:[UIColor greenColor]];
            break;
        }
    }
}

答案 1 :(得分:0)

此解决方案应适用于快速4和5。

我正在使用xcode系统映像,因此在运行此代码时无需替换映像

// Add this to your Table View Controller

// Step 1

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    if(tableView.cellForRow(at: indexPath)?.imageView?.image == UIImage(systemName:"checkmark.circle")) {
        tableView.cellForRow(at: indexPath)?.imageView?.image = UIImage(systemName:"circle")
    } else {
        tableView.cellForRow(at: indexPath)?.imageView?.image = UIImage(systemName:"checkmark.circle")
    }
}

//Step 2

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    cell.textLabel?.text = employeeValues[indexPath.row]
    cell.imageView?.image = UIImage(systemName:"circle")

    return cell
}