2015-10-12 4 views
-1

У меня возникла проблема с добавлением элементов в NSMutableArray и написанием их в tableView. После этого кода он должен писать в элементах tableView от @ "0" до @ "19", но он показывает мне от 0 до 13 и после этого в неправильном порядке.NSMutableArray не добавляет элементы справа

Вот скриншот:

screenshot

Как написать их в правильном порядке до конца массива?

- (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

Переместить setText: метод снаружи (внизу внизу) (cell == nil). – 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; 
} 

Вы не хотите установить содержание только тогда, когда создается клетка. Ячейки повторно используются, одна и та же ячейка будет использоваться позже для другого индекса, поэтому вам также необходимо настроить содержимое в этом случае.

Смежные вопросы