Ios 在视图外的单元格中重新加载UITableview数据
我的问题是,我有一个包含10多个项目的tableview,视图中只有6个单元格,但是当加载数据并滚动表格时,单元格7中没有数据。如何重新加载该单元格中的数据 上面的代码是我正在使用的,它不工作 ---编辑---Ios 在视图外的单元格中重新加载UITableview数据,ios,iphone,uitableview,cocoa-touch,reload,Ios,Iphone,Uitableview,Cocoa Touch,Reload,我的问题是,我有一个包含10多个项目的tableview,视图中只有6个单元格,但是当加载数据并滚动表格时,单元格7中没有数据。如何重新加载该单元格中的数据 上面的代码是我正在使用的,它不工作 ---编辑--- 这通常是cellforrowatinexpath:的回收/重用机制中的误用 滚动时会重复使用单元格,以避免分配太多UITableViewCells。因此,在这两种情况下,您都需要在cellForRowAtIndexPath:中设置单元格的内容,如果单元格刚刚被分配(cell==nil)或
这通常是
cellforrowatinexpath:
的回收/重用机制中的误用
滚动时会重复使用单元格,以避免分配太多UITableViewCells。因此,在这两种情况下,您都需要在
cellForRowAtIndexPath:
中设置单元格的内容,如果单元格刚刚被分配(cell==nil)或者单元格被重用(cell!=nil)。这里可能是您的tableView:cellForRowAtIndexPath:出错。你能发布你的代码吗?其余的呢?你到底在哪里把文字放在标签上,把图片放在图片视图上?对不起,我这么做太匆忙了,哈哈,我必须补充一点,我在另一个应用程序中使用了相同的代码,我没有这个问题。那么为什么单元格1-6和8-10会加载而不是7呢?我必须补充一点,我在另一个应用程序中使用相同的代码,我没有这个问题,正是因为我解释的原因。因为这些第一个单元格已分配,您将其内容填入cell==nil
案例中(因为没有现有的UITableViewCell
已分配但未使用和可重用,所以dequeueCellWithIdentifier
返回nil
),但当您滚动时,这些单元格将被重用(回收)显示其他内容,因此在本例中,您进入单元格=nil
情况,您的代码中可能存在问题。在代码中放置断点,以便更好地了解在这两种情况下执行代码中的路径。特别是,您应该在if
语句之外填充标签、图像视图等内容(以及在一个单元格和另一个单元格之间更改的所有内容),以便在这两种情况下填充单元格,然而,这些细胞已经被回收或刚刚分配。
[self.countryList reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:6 inSection:0], nil] withRowAnimation:UITableViewRowAnimationNone]; // or NO ;)
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];
}