我正在创建一个应用程序来显示列表,每个列表都有一定数量的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; } @endAlternatively, you could just use a BOOL:
@interface ClassCell @property BOOL hasImageViews; @endand 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.
更多推荐
发布评论