Проблема с настраиваемыми ячейками TableViewCells при отображении таблицы из NSMutableArray - PullRequest
0 голосов
/ 25 марта 2011

У меня странная проблема - в моем коде я получаю данные из Интернета в формате XML и заполняю NSMutableArray (всего 34 элемента).

Эти данные должны отображаться в TableView.Я создал собственный TableViewCell для этого.Однако он всегда отображает только первые 10 элементов (0-9), затем 11-й элемент изменится, когда вы прокрутите таблицу вверх (начиная с 11 и изменив себя на 12, 13, 14 ...), а затем отобразитснова первые 10 элементов.

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {

    return [myCurrencies count];
}

    - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *myIdentifier = @"CurrencyDisplayCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier];

        if (cell == nil) {
            [[NSBundle mainBundle] loadNibNamed:@"CurrencyTableViewCell" owner:self options:nil];
            cell = tableViewCell;
            cell.accessoryType = UITableViewCellAccessoryNone;
            self.tableViewCell = nil;
        }

        NSLog(@"%i",indexPath.row);

        actualRate.text = [NSString stringWithFormat:@"%@",[myRates objectAtIndex:indexPath.row]];
        currencyCode.text = [NSString stringWithFormat:@"(%i) %@",indexPath.row,[myCurrencies objectAtIndex:indexPath.row]];
        countryFlag.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.gif",[myCurrencies objectAtIndex:indexPath.row]]];

        return cell;
    }

Однако, когда я просто устанавливаю текстовую метку стандартной ячейки, все в порядке, и я отображаю 34 строки таблицы с номерами 0 - 33:

cell.textLabel.text = [NSString stringWithFormat:@"%i",indexPath.row]; 

Есть идеи?

1 Ответ

0 голосов
/ 25 марта 2011

неверная инициализация вашей ячейки

Прежде всего назовите все ваши метки и представления изображений в Интерфейсном Разработчике с помощью тега, например, currentRate - тег 101, тег currencyCode 102, тег countryFlag 103

, затем

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *myIdentifier = @"CurrencyDisplayCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier];

        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"CurrencyTableViewCell" owner:self options:nil] lastObject];
        }

        NSLog(@"%i",indexPath.row);

        [((UILabel *)[cell viewWithTag:101]) setText:[NSString stringWithFormat:@"%@",[myRates objectAtIndex:indexPath.row]]];
        [((UILabel *)[cell viewWithTag:102]) setText:[NSString stringWithFormat:@"(%i) %@",indexPath.row,[myCurrencies objectAtIndex:indexPath.row]]];
        [((UIImageView *)[cell viewWithTag:103]) setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@.gif",[myCurrencies objectAtIndex:indexPath.row]]]];

        return cell;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...