用自定义图像替换UITableView的默认复选标记 [英] Replace the default checkmark for UITableView with a custom image

查看:90
本文介绍了用自定义图像替换UITableView的默认复选标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想替换UITableViewCell配件设置为时显示的默认复选标记图像:UITableViewCellAccessoryCheckmark。

I'd like to replace the default checkmark image that is shown when a UITableViewCell's accessory is set to: UITableViewCellAccessoryCheckmark.

所以我还是想写:

[cell setAccessoryType:UITableViewCellAccessoryCheckmark];

但是我希望显示自己的图像而不是默认图像。

But I'd like my own image to be shown rather than the default.

非常感谢任何帮助。

推荐答案

方法-1:

我认为你可以使用

假设你有一个带有复选标记图像的UIButton。

Suppose you have a UIButton with checkmark image.

On cellForRowAtIndexPath:

On cellForRowAtIndexPath:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.frame = frame;
[button setBackgroundImage:image forState:UIControlStateNormal];

[button addTarget:self action:@selector(checkButtonTapped:event:)  forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;

checkButtonTapped:event:Method:

- (void)checkButtonTapped:(id)sender event:(id)event
{
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
    if (indexPath != nil)
    {
        [self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
    }
}

accessoryButtonTappedForRowWithIndexPath:Method

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row];

    BOOL checked = [[item objectForKey:@"checked"] boolValue];

    [item setObject:[NSNumber numberWithBool:!checked] forKey:@"checked"];

    UITableViewCell *cell = [item objectForKey:@"cell"];
    UIButton *button = (UIButton *)cell.accessoryView;

    UIImage *newImage = (checked) ? [UIImage imageNamed:@"unchecked.png"] : [UIImage imageNamed:@"checked.png"];
    [button setBackgroundImage:newImage forState:UIControlStateNormal];
}

我已从此链接获取以上代码:

I have taken above code from this link:

为iPhone中的UITableView实现自定义附件视图

方法-2:

还有一种方法可以使用自定义tableView单元格。

Also there is a way to make it using custom tableView cell.

我认为你必须制作一个自定义单元格在单元格的右侧添加一个imageView。

I think you have to make a custom cell and add an imageView on right hand side of the cell.

此imageView将保留您想要的图像。

This imageView will then hold the image you want.

在didSelectRowAtRowAtIndexPath:您可以添加图像并根据点击次数删除。

On didSelectRowAtRowAtIndexPath: you can add the image and remove based on number of taps.

如果您需要更多帮助,请告诉我。

Do let me know if you want more help.

希望这会有所帮助。

这篇关于用自定义图像替换UITableView的默认复选标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆