为什么我的图像会在表格视图中一次又一次地加载?(Why are my images loaded again and again in a table view?)

编程入门 行业动态 更新时间:2024-10-28 10:23:26
为什么我的图像会在表格视图中一次又一次地加载?(Why are my images loaded again and again in a table view?)

我正在创建一个应用程序来显示列表,每个列表都有一定数量的x图像显示在表格单元格中。

为了显示图像,我必须动态创建UIImageView并通过for循环将图像加载到单元格中(取决于从服务器调用接收的数据)。

现在,我可以动态添加图像,但是当我滚动表视图时, cellForRowAtIndexPath函数再次运行,图像再次加载到单元格中,因此创建的图像视图比实际数据更多。

我希望在单元格中保持图像计数不变,并且不希望在表格滚动时在单元格中创建更多图像。

这是功能代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *Cellidentifier1 = @"ClassCell"; ClassCell *cell = [tableView dequeueReusableCellWithIdentifier:Cellidentifier1 forIndexPath:indexPath]; // Configure the cell... long row = [indexPath row]; for (int t = 0; t<individualSports.count; t++) { UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake((250/10*count+10), 125, 20, 20)]; [imageView setImage:[UIImage imageNamed:@"cricket_unselected.png"]]; [cell addSubview:imageView]; } return cell; }

I am creating an app to display listings and each listing has an amount of x images to be displayed in a table cell.

To display the images, I have to create the UIImageView dynamically and load the images into the cell via a for loop (depending on the data received from a server call).

Now, I am able to add the images dynamically, but when I scroll the table view, the cellForRowAtIndexPath function runs again and the images are loaded again into the cell, hence creating more image views than the actual data.

I want to keep the image count constant in the cell and do not want to create more images in the cell as the table scrolls.

Here is the function code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *Cellidentifier1 = @"ClassCell"; ClassCell *cell = [tableView dequeueReusableCellWithIdentifier:Cellidentifier1 forIndexPath:indexPath]; // Configure the cell... long row = [indexPath row]; for (int t = 0; t<individualSports.count; t++) { UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake((250/10*count+10), 125, 20, 20)]; [imageView setImage:[UIImage imageNamed:@"cricket_unselected.png"]]; [cell addSubview:imageView]; } return cell; }

最满意答案

您的ClassCell可以为您解决此问题。 如果它用于其他目的,只需再次进行子类化;

@implementation ClassCell // or a new ClassCell subclass - (id)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { // for loop.. } return self; } @end

或者,您可以使用BOOL :

@interface ClassCell @property BOOL hasImageViews; @end

接着:

if (!cell.hasImageViews) { cell.hasImageViews = YES; // for loop.. }

旁注 :我不确定为什么要将相同的图片多次添加到单个单元格中; 您是否更有可能想要使用某种复选框? 此外,您使用t迭代,但随后应用带有count的帧,这意味着所有图像视图彼此重叠,因为它们在cell具有相同的帧。

Your ClassCell could take care of this for you. If it is used for other purposes, just subclass it again;

@implementation ClassCell // or a new ClassCell subclass - (id)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { // for loop.. } return self; } @end

Alternatively, you could just use a BOOL:

@interface ClassCell @property BOOL hasImageViews; @end

and then:

if (!cell.hasImageViews) { cell.hasImageViews = YES; // for loop.. }

Side note: I'm not really sure why you want to add the same picture that many times to a single cell, though; isn't it more likely that you want to use a checkbox of some kind? Also, you're iterating with t, but then applying the frame with count, meaning that all of your image views are overlapped on each other because they have the same frame within the cell.

更多推荐

本文发布于:2023-08-03 16:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1393502.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:会在   视图   表格   图像   加载

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!