ios-无法理解为什么在添加新项目时,项目列表会在第一个和最后一个位置显示第一个项目的值

ios-无法理解为什么在添加新项目时,项目列表会在第一个和最后一个位置显示第一个项目的值,ios,ios5,uitableview,Ios,Ios5,Uitableview,我有一个从远程数据库获取的字符串列表,它们显示得很好。然后,当我添加一个字符串时,这个新字符串会被添加到数据库中,但是当需要在屏幕上显示它时,由于某种原因,它会同时显示第一个和最后一个项目中的第一个项目的值 以下是我正在做的: // CREATING EACH CELL IN THE LIST - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

我有一个从远程数据库获取的字符串列表,它们显示得很好。然后,当我添加一个字符串时,这个新字符串会被添加到数据库中,但是当需要在屏幕上显示它时,由于某种原因,它会同时显示第一个和最后一个项目中的第一个项目的值

以下是我正在做的:

// 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];
            });
这是名为

-(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];
    }
}
有人能告诉我为什么最后一项显示为第一项的值吗?我真的被难住了


谢谢

我猜cellTitleArray是一个实例变量?如果是这样,第二次调用loadTitleStrings时(将新字符串添加到远程数据库并再次获取所有字符串后),cellTitleArray将是您当前使用的一个。也许你可以再加上所有的字符串。如果是这种情况,您可以在foreach循环in-loadTitleStrings之前添加[CellTitleAry removeAllObjects]

也许在你的第二个字符串中发生了一些错误。我认为按照您的代码执行不是一个好主意:

items_array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

if(!error){
   [self loadTitleStrings];
}
您向error参数传递了一个nil,当然错误将为nil。当错误发生时,您无法得到通知。尝试此操作以查看是否存在错误:

NSError *error = nil;
items_array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

if(!error){
   [self loadTitleStrings];
} else {
    NSLog(@"%@",error);
}