2015-10-12 40 views
-1

我有一个向NSMutableArray添加元素并将它们写入到tableView中的问题。在这段代码之后,它应该从@“0”到@“19”的tableView元素中写入,但是它会显示我从0到13,然后以错误的顺序显示。NSMutableArray不要添加元素正确

下面是截图:

​​

如何把它们写在正确的顺序排列的结束?

- (void)refreshBottom{ 
    int p = 0; 
    for (int i = p; i< p + 20; i++) { 
     [cells addObject:@(i)]; 
    } 
    [self.refreshControl endRefreshing]; 
    [_timeline_table reloadData]; 
} 

单元格是NSMutableArray

UITableView的数据源

- (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]; 
     cell.selectionStyle = UITableViewCellSelectionStyleDefault; 
     [cell.textLabel setText:[NSString stringWithFormat:@"%@",[cells objectAtIndex:indexPath.row]]]; 
    } 
    return cell; 
} 
+0

发布你的'UITableViewDataSource'的代码。 – rckoenes

+1

移动(cell == nil)情况下的setText:方法外部(向下)。 – EDUsta

+0

@EDUsta的作品!谢谢! –

回答

0

您需要设置if外的单元格的值:

- (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]; 
     cell.selectionStyle = UITableViewCellSelectionStyleDefault; 
    } 

    [cell.textLabel setText:[NSString stringWithFormat:@"%@",[cells objectAtIndex:indexPath.row]]]; 

    return cell; 
} 

你不想设置仅在创建单元格的内容。单元格被重用,相同的单元格将在以后用于其他索引,因此您也必须在这种情况下设置内容。