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

 正在减肥的小小_519 发布于 2023-01-30 10:14

当用户选择行时,我想更改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"]];

    //..
}

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

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

提前致谢.

1 个回答
  • 使用这段代码:

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

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

    2023-01-30 10:16 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有