用自定义图像替换UITableView的默认复选标记

时间:2012-04-18 12:04:42

标签: objective-c ios uitableview

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

所以我还是想写:

[cell setAccessoryType:UITableViewCellAccessoryCheckmark];

但是我想要显示自己的图像而不是默认图像。

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:26)

答案 1 :(得分:4)

方式-1:

我认为你可以使用

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

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:方法:

- (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:方法

- (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];
}

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

Implement a Custom Accessory View For UITableView in iPhone

方式-2:

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

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

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

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

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

希望这有帮助。

答案 2 :(得分:2)

看一下这段代码:

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
...
    NSMutableDictionary *item = [dataArray bjectAtIndex:indexPath.row];
    cell.textLabel.text = [item objectForKey:@"text"];

    [item setObject:cell forKey:@"cell"];

    BOOL checked = [[item objectForKey:@"checked"] boolValue];
    UIImage *image = (checked) ? [UIImage imageNamed:@"checked.png"] : [UIImage imageNamed:@"unchecked.png"];

    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;
    ...
}

    - (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];
       }
    }

    - (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];
    }

reference