ios - UITableView 在单元格选择上更改图像并重置其他图像

标签 ios objective-c uitableview uiimageview

我想在用户选择行时更改 UITableViewCell 内的 UIImage。那现在太难了。我可以在 didSelectRowAtIndex 中做到这一点。但是,这就是问题所在,我希望其他单元格(每个 cell 都有标准图像)再次具有标准图像。 (如果单元格再次具有标准图像,则所选图像应该被“标记”(意味着未标记图像将被标记图像替换,我已经有代码)

将其视为您从 HTML 中了解到的 Checkbox

这是我的尝试:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView cellForRowAtIndexPath:indexPath];
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:YES];

    //Little flashing animation
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [UIView animateWithDuration:2.0f animations:^{

    } completion:^(BOOL finished) {
        ;
    }];

    //Mark the selected cell
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    ALog(@"%i", cell.imageView.tag);
    ALog(@"%i", [[[tableView cellForRowAtIndexPath:indexPath] imageView] tag]);

    UIImageView *iconimgView = (UIImageView *)[[tableView cellForRowAtIndexPath:indexPath] viewWithTag:TAG_IMAGEMARKERCELL];
    [iconimgView setImage:[UIImage imageNamed:@"circle-answer-marked.png"]];

    //..
}

UIImageView 定义在这里:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        //Add this to cell only once, not everytime a cell is displayed! (Everyhing goes in here)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        [[cell textLabel] setNumberOfLines:0]; // unlimited number of lines
        [[cell textLabel] setLineBreakMode:NSLineBreakByWordWrapping];
        [[cell textLabel] setFont:[UIFont fontWithName:@"PTSans-Regular" size:33 / 2]];
        [[cell textLabel] setTextColor:[self colorFromHexString:COLOR_DEFAULT_TEXT_COLOR]];
        cell.indentationLevel = cell.indentationLevel + 2;

        //        //Marker image
        imgCell_Marker = [[UIImageView alloc]initWithFrame:CGRectMake(10, 22, 20, 20)];
        imgCell_Marker.image=[UIImage imageNamed:@"circle-answer.png"];
        imgCell_Marker.tag = TAG_IMAGEMARKERCELL;
        ALog(@"%@", imgCell_Marker);
        [cell.contentView addSubview:imgCell_Marker];
    }

        //..
        return cell;
}

奇怪的是,UIImageView 有一个预定义的标签,但如果我想在 didSelectRowAtIndexPath 中使用它,它总是“0”。 (标签的默认值)

提前致谢。

最佳答案

使用这段代码:

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{   
    [tableView reloadData];
    UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    cell.imageView.image = [UIImage imageNamed:@"image.png"];
}

首先,您将所有单元格设置为默认状态,然后更改当前单元格的图像。

关于ios - UITableView 在单元格选择上更改图像并重置其他图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21483572/

相关文章:

objective-c - OCUnit 中的简化断言

ios - TableView 单元格到另一个 View Controller

ios - 像素化 UIImage 返回具有不同大小的 UIImage

ios - 当循环到未知的 JSON 数据时,如何在 allKeys 上捕获错误?

iphone - 根据另一个选择器值更改选择器值

iphone - 如何检测长按?

ios - Swift 4 : Xib with collection view that reuse cell from storyboard, 内部导出为零

ios - UITableview ios 中的水平和垂直 ScrollView

iphone - Phonegap:适用于iOS4的应用

ios - 拍摄照片时未使用 AVCaptureDevice 的设置