Простой UITableView отображает странный порядок данных - PullRequest
0 голосов
/ 12 декабря 2011

Я создаю следующие данные на viewDidLoad:

- (void)viewDidLoad {
    [super viewDidLoad];

    food = [[NSMutableArray alloc] initWithObjects: @"Harvey's",@"McCormicks",@"Subway",nil];
    gifts = [[NSMutableArray alloc] initWithObjects:@"The gift Shop",@"Lilo Gifts",@"Prezzies for All", nil];
    services = [[NSMutableArray alloc] initWithObjects:@"Hair Snippers",@"Laundro-mat",@"Accountants",@"Shoe fitters", nil];
    hotel = [[NSMutableArray alloc] initWithObjects:@"Hotel Paradiso",@"Dick & Doms B&B",@"Super Hotel",@"Castle B&B", nil];
    pubs = [[NSMutableArray alloc] initWithObjects:@"The Snuggly Duckling",@"Beer's Arms",@"Merlins Beard", @"Cheers", nil];


}

Вот мои методы источника данных:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 5;
}

- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return NSLocalizedString(@"Food & Restaraunts", nil);
        case 1:
            return NSLocalizedString(@"Shopping/ Gifts", nil);
        case 2:
            return NSLocalizedString(@"Services", nil);
        case 3:
            return NSLocalizedString(@"Hotels / B&B's", nil);
        case 4:
            return NSLocalizedString(@"Pubs",nil);

        default:
            return nil;
    }



}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return 3;
        case 1:
            return 3;
        case 2:
            return 4;
        case 3:
            return 4;
        case 4:
            return 4;
        //default:
        //  return 1;

    }



}


// Customize the appearance of table view cells.
- (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] autorelease];
    //mine  

        switch (indexPath.section) {
    case 0:
        [[ cell textLabel]
         setText:[food objectAtIndex:indexPath.row]];
        break;
    case 1:
        [[ cell textLabel]
         setText:[gifts objectAtIndex:indexPath.row]];
        break;
    case 2:
        [[ cell textLabel]
        setText:[services objectAtIndex:indexPath.row]];
        break;
    case 3:
        [[ cell textLabel]
        setText:[hotel objectAtIndex:indexPath.row]];
        break;
    case 4:
        [[ cell textLabel]
        setText:[pubs objectAtIndex:indexPath.row]];
        break;

    } 

    return cell;
}

}

Что он на самом деле делает, так это отображает первые 10 строк правильно, а затем повторяет список снова, если я добавлю больше строк в первые пару разделов, которые он повторяет ранее в таблице ... Я очистил кэш и т. Д., Но ничего не работает.

1 Ответ

1 голос
/ 12 декабря 2011

Это потому, что вы устанавливаете содержимое своих повторно используемых ячеек только один раз.

- (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] autorelease];}
//mine  

    switch (indexPath.section) {
case 0:
    [[ cell textLabel]
     setText:[food objectAtIndex:indexPath.row]];
    break;
case 1:
    [[ cell textLabel]
     setText:[gifts objectAtIndex:indexPath.row]];
    break;
case 2:
    [[ cell textLabel]
    setText:[services objectAtIndex:indexPath.row]];
    break;
case 3:
    [[ cell textLabel]
    setText:[hotel objectAtIndex:indexPath.row]];
    break;
case 4:
    [[ cell textLabel]
    setText:[pubs objectAtIndex:indexPath.row]];
    break;



return cell;}

Я не очень хорош в постформатировании.Вы должны установить содержимое своей ячейки после извлечения ее из «стека повторного использования».

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