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

pas*_*skl 4 objective-c uitableview uiimageview ios

当用户选择行时,我想更改a UIImage里面的内容UITableViewCell.多数民众赞成在现在太难了.我可以做得didSelectRowAtIndex很好.然而,继续问题,我希望其他单元格(每个单元格cell都有标准图像)再次获得标准图像.(如果单元格再次具有标准图像,则应"标记"所选图像(意味着未标记的图像将被标记的图像替换,我已经有了代码)

把它看成是Checkbox你所知道的HTML.

这是我的尝试:

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

    //..
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

奇怪的是,UIImageViews有一个预定义的tag但是如果我想在didSelectRowAtIndexPath它的总是"0" 上得到它.(默认值tag)

提前致谢.

eto*_*toy 7

使用这段代码:

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

首先,将所有单元格设置为默认条件,然后更改当前单元格的图像.