UITableView由于某种原因正在加载相同的单元格
我有一个TableView加载自定义单元并从服务器上的JSON字符串加载数据.JSON字符串被解析为包含14个ID(id_array)的数组.
I have a TableView loading a custom cell and loading the data from a JSON string on a server. The JSON string is parsed to an array of 14 IDs (id_array).
如果 cell == nil
,那么我正在使用 [id_array objectAtIndex:indexPath.row]
获取ID并从中获取有关该行的更多信息服务器,并设置单元格的标签和图像.
If cell==nil
, then I'm using [id_array objectAtIndex:indexPath.row]
to get an ID and fetch some more info about for that row from a server, and set up the cell's labels and image.
运行应用程序时,UITableView会加载可见行[0,1,2,3,4](单元格高度为70像素).向下滚动TableView时,将加载行[5]并从服务器获取数据,但是问题在于,超出这一点-TableView将重复这6行,而不是从服务器为新行请求新数据...
When running the app, the UITableView is loading the visible rows [0,1,2,3,4] (cell height is 70px). When scrolling down the TableView, row [5] is loaded and fetching data from the server, but the problem is that beyond that point - the TableView is repeating those 6 rows instead of requesting new data from the server for the new rows...
但是它确实请求第[5]行的新数据,该数据在应用程序首次运行时不可见(且未加载).
But it does request new data for row [5], which is not visible (and not loaded) when the app first runs.
任何人都知道为什么会这样吗?谢谢!
Anyone have any idea why is this happening? Thanks!
这是我的cellForRowAtIndexPath方法
Here is my cellForRowAtIndexPath method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (CustomCell *)currentObject;
NSString *appsURL = [NSString stringWithFormat:@"http://myAPI.com?id=%@",[app_ids objectAtIndex:indexPath.row]];
NSLog(@"row -> %d | id -> %@",indexPath.row,[app_ids objectAtIndex:indexPath.row]);
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:appsURL]];
[cell updateAppInfoForCellWithRequest:request];
break;
}
}
}
// Configure the cell...
return cell;
}
如果仅当 cell == nil
时才设置数据,那么这就是您的问题.UITable构建了一个表格视图单元格的缓存,如果单元格为nil,则仅创建一个新的缓存.因此,每次必须设置数据,即在 cell == nil
块之外.
If you are setting data only if cell==nil
, then that is your issue. UITable builds a cache of table view cells, only creating a new one if cell is nil. Therefore, you must set your data each time, i.e. outside of the cell==nil
block.
以下示例显示了该过程.首先,从池中获取一个单元,如果没有可用单元,则创建一个新单元.为相应的行设置单元格的值.
The example below shows that process. First, grab a cell from the pool, if there isn't a free cell, create a new one. Set the cell's values for the appropriate row.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
id someData = [id_array objectAtIndex:indexPath.row]
cell.textLabel.text = [someData someString];
return cell;
}