6

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

所以我还是想写:

[cell setAccessoryType:UITableViewCellAccessoryCheckmark];

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

任何帮助深表感谢。

4

3 回答 3

30
myUITableViewCell.accessoryView = myImageView;

然后把你的图片放到 UIImageView 中。

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITableViewCell_Class/Reference/Reference.html#//apple_ref/occ/cl/UITableViewCell

于 2012-04-18T12:09:41.370 回答
4

方法一:

我认为你可以使用

假设您有一个带有复选标记图像的 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:事件:方法:

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

附件ButtonTappedForRowWithIndexPath:方法

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

我从这个链接中获取了上面的代码:

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

方法二:

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

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

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

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

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

希望这可以帮助。

于 2012-04-18T12:17:13.137 回答
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];
    }

参考

于 2012-04-18T12:06:39.870 回答