gpt4 book ai didi

ios - 在 View 之外的单元格中重新加载 UITableview 数据

转载 作者:行者123 更新时间:2023-11-29 04:55:43 25 4
gpt4 key购买 nike

[self.countryList reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:6 inSection:0], nil] withRowAnimation:UITableViewRowAnimationNone]; // or NO ;)

我的问题是,我有一个包含 10 多个项目的表格 View , View 中只有 6 个单元格,但是当加载数据并且滚动表格时,单元格 7 中没有数据。如何重新加载该 1 个单元格中的数据?

上面的代码是我正在使用的,但它不起作用!

---编辑---

if (cell == nil) {  

cell =[[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:identifier]autorelease];
//[cell setBackgroundColor: [UIColor clearColor]];




mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(52.0, 0.0, 200.0, 20.0)] autorelease];
mainLabel.tag = 1;
mainLabel.font = [UIFont systemFontOfSize:14.0];
mainLabel.textAlignment = UITextAlignmentLeft;
mainLabel.textColor = [UIColor blackColor];
mainLabel.opaque = YES;
mainLabel.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:mainLabel];

revenue = [[[UILabel alloc] initWithFrame:CGRectMake(52.0, 20.0, 150.0, 20.0)] autorelease];
revenue.tag = 3;
revenue.font = [UIFont systemFontOfSize:14.0];
revenue.textAlignment = UITextAlignmentLeft;
revenue.textColor = [UIColor blackColor];
revenue.opaque = YES;
revenue.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:revenue];

promos = [[[UILabel alloc] initWithFrame:CGRectMake(252.0, 20.0, 150.0, 20.0)] autorelease];
promos.tag = 4;
promos.font = [UIFont systemFontOfSize:14.0];
promos.textAlignment = UITextAlignmentLeft;
promos.textColor = [UIColor blackColor];
promos.opaque = YES;
promos.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:promos];

updates = [[[UILabel alloc] initWithFrame:CGRectMake(252.0, 0.0, 200.0, 20.0)] autorelease];
updates.tag = 5;
updates.font = [UIFont systemFontOfSize:14.0];
updates.textAlignment = UITextAlignmentLeft;
updates.textColor = [UIColor blackColor];
updates.opaque = YES;
updates.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:updates];

photo = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 50.0, 50.0)] autorelease];
photo.contentMode= UIViewContentModeScaleToFill;
photo.tag = 2;
photo.opaque = YES;
photo.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:photo];

}
else {
cell.clearsContextBeforeDrawing=YES;
mainLabel = (UILabel *)[cell.contentView viewWithTag:1];
photo = (UIImageView *)[cell.contentView viewWithTag:2];
promos=(UILabel *)[cell.contentView viewWithTag:4];
revenue=(UILabel *)[cell.contentView viewWithTag:3];
updates=(UILabel *)[cell.contentView viewWithTag:5];


if(photo.image){
photo.image=nil;
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imageURL =[NSString stringWithString:[(arrayResults*)[tweets objectAtIndex:indexPath.row] myCountryIcon]];

NSArray *myArray = [imageURL componentsSeparatedByString: @"/"];
NSString *fileName = [NSString stringWithString:[myArray lastObject]];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/AppIcons"];
NSString* path = [dataPath stringByAppendingPathComponent:fileName];
UIImage* image = [UIImage imageWithContentsOfFile:path];
if (!image) {

UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES;
// Create an array with the URL and name.
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:fileName,imageURL, nil ];


[self performSelectorInBackground:@selector(loadImageInBackground:) withObject:arr];
[arr release];


}
else{
photo.image=image;


}
NSString*promostext=[[NSString alloc] initWithFormat:@"promos:%@",[[(arrayResults*)[tweets objectAtIndex:indexPath.row] mypromos]stringValue] ];
NSString*revenuetext=[[NSString alloc] initWithFormat:@"revenue:%@",[(arrayResults*)[tweets objectAtIndex:indexPath.row] myrevenue] ];
NSString*updatestext=[[NSString alloc] initWithFormat:@"updates:%@",[[(arrayResults*)[tweets objectAtIndex:indexPath.row] myupdates]stringValue] ];

mainLabel.text=[(arrayResults*)[tweets objectAtIndex:indexPath.row] myCountryName];
promos.text=promostext;
revenue.text=revenuetext;
updates.text=updatestext;
[promostext release];
[revenuetext release];
[updatestext release];

}

最佳答案

这通常是 cellForRowAtIndexPath: 回收/重用机制的误用。

滚动时会重复使用单元格,以避免分配过多的 UITableViewCell。因此,在这两种情况下,您都需要在 cellForRowAtIndexPath: 中设置单元格的内容,无论单元格刚刚被分配 (cell==nil) 还是单元格已被重用 (cell!=nil) )。

关于ios - 在 View 之外的单元格中重新加载 UITableview 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8027586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com