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

ios – UITableView在单元格选择上更改图像并重置其他图像,第1张

概述当用户选择行时,我想更改UITableViewCell内的UI Image.多数民众赞成在现在太难了.我可以在didSelectRowAtIndex中做到这一点就好了.然而,并且继续问题,我希望其他单元格(每个单元格具有标准图像)再次具有标准图像. (如果单元格再次具有标准图像,则应该“标记”所选图像(意味着未标记的图像将被标记的图像替换,我已经有了代码) 将其视为您从HTML中了解的复选框. 这 当用户选择行时,我想更改UItableVIEwCell内的UI Image.多数民众赞成在现在太难了.我可以在dIDSelectRowAtIndex中做到这一点就好了.然而,并且继续问题,我希望其他单元格(每个单元格具有标准图像)再次具有标准图像. (如果单元格再次具有标准图像,则应该“标记”所选图像(意味着未标记的图像将被标记的图像替换,我已经有了代码)

将其视为您从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有一个预定义的标签,但如果我想在dIDSelectRowAtIndexPath中得到它,那么它总是“0”. (标签的默认值)

提前致谢.

解决方法 你在许多方面都有问题.

首先,确定应显示什么图像的信息应该是模型的一部分.因此,当选择一行时,在模型中设置一个标记,表示该项已被标记.同时,找到标记的其他项目并取消标记.现在,重新加载已更改的行.在cellForRowAtIndexPath中,更新请求的单元格以根据模型中的标记信息设置标准或替代图像.

其次,对于你使用标签 – 你在imgCell_Marker和imageVIEw之间混淆了.当你这样做时:

ALog(@"%i",[[[tableVIEw cellForRowAtIndexPath:indexPath] imageVIEw] tag]);

您正在查询默认的imageVIEw,因此标记始终为零.您应该使用vIEwWithTag:TAG_IMAGEMARKERCELL来查找所需的实际图像视图.

总结

以上是内存溢出为你收集整理的ios – UITableView在单元格选择上更改图像并重置其他图像全部内容,希望文章能够帮你解决ios – UITableView在单元格选择上更改图像并重置其他图像所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1057404.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存