我有一个UITableView由NSArray.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
id item = [self.data objectAtIndex:indexPath.row];
cell.item = item;
return cell;
}
很标准。现在的问题是,它reloadData会同步请求numberOfSections,numberOfRows但会cellForRow异步调用。所以有时,当cellForRowAtIndexPath被调用时,数据数组已经改变,因此[self.data objectAtIndex:indexPath.row]会出现越界异常并使应用程序崩溃。我该如何避免这种情况?
请注意,每次设置data数组时,我也会调用[self.tableView reloadData].