我有一个从远程数据库中获取的字符串列表,它们显示得很好。然后,当我添加一个字符串时,该新字符串会很好地添加到数据库中,但是当需要在屏幕上显示它时,由于某种原因,它会同时显示第一项和最后一项中的第一项的值。
这是我正在做的事情:
// CREATING EACH CELL IN THE LIST
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"business";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17];
cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
}
cell.textLabel.text = [cellTitleArray objectAtIndex:indexPath.row];
// CLOSE THE SPINNER
[spinner stopAnimating];
// return the cell for the table view
return cell;
}
当从数据库中检索数据时,这就是我所做的:
dispatch_sync(dispatch_get_main_queue(), ^{
items_array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
if(!error){
[self loadTitleStrings];
}
[self.itemList reloadData];
});
这是调用的 loadTitleStrings
-(void)loadTitleStrings
{
if(!standardUserDefaults)
standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSString *is_private = [standardUserDefaults objectForKey:@"is_private"];
if(!cellTitleArray)
{
cellTitleArray = [NSMutableArray array];
}
for(NSDictionary *dictionary in items_array)
{
NSString *tcid = [dictionary objectForKey:@"comment_id"];
[theArray addObject:tcid];
NSString *string;
if(!is_private || [is_private isEqualToString:@"0"])
{
string = [NSString stringWithFormat:@"%@: %@", [dictionary objectForKey:@"first_name"], [dictionary objectForKey:@"comment"]];
}
else
{
string = [NSString stringWithFormat:@"%@", [dictionary objectForKey:@"comment"]];
}
[cellTitleArray addObject:string];
}
}
谁能说出为什么最后一项显示为第一项的值?我真的很难过!
谢谢!