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

前端之家收集整理的这篇文章主要介绍了objective-c – 用自定义图像替换UITableView的默认复选标记前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想替换UITableViewCell附件设置为时显示的默认复选标记图像:UITableViewCellAccessoryCheckmark.

所以我还是想写:

[cell setAccessoryType:UITableViewCellAccessoryCheckmark];

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

任何帮助深表感谢.

解决方法

方法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:您可以添加图像并根据点击次数删除.

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

希望这可以帮助.

猜你在找的C&C++相关文章