При прокрутке таблицы назад ячейки снова становятся пустыми? - PullRequest
2 голосов
/ 04 сентября 2011

После загрузки таблицы с пользовательскими ячейками все выглядит хорошо, а прокрутка вниз - в порядке. Когда я прокручиваю вверх, ячейки выше кажутся пустыми. На самом деле они полностью функциональны, так как их все еще можно выбрать, а затем они переходят на страницы с подробностями. Код cellForRowAtIndexPath выполняется для возвращаемой строки и возвращает ячейку, как и следовало ожидать с правильными деталями. Это просто не отображается. Любые мысли / помощь будут оценены.

Код ниже - то, что я использовал.

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


    static NSString *CustomCellIdentifier = @"CustomCellIdentifier";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    if (cell == nil) {

        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        for (id oneObject in nib) {
            if ([oneObject isKindOfClass:[CustomCell class]]) {
                cell = (CustomCell *)oneObject;
            }
        }
    }

    // Configure the cell.


    Book *aBook = [appDelegate.sortedArray objectAtIndex:indexPath.row];


    //name
    NSString *trimmedString = [aBook.Name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    cell.nameLabel.text = trimmedString;
    //catagory
    NSString *trimmedExperience = [aBook.PrimaryExperience stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    if ([trimmedExperience isEqualToString:@"1"]) {
        cell.catagoryLabel.text = @"None";
    }
    else if([trimmedExperience isEqualToString:@"2"]){
        cell.catagoryLabel.text = @"Limited";
    }
    else if ([trimmedExperience isEqualToString:@"4"]) {
        cell.catagoryLabel.text = @"Full";

    }
    else {
        cell.catagoryLabel.text = @"";

    }




    cell.distanceLabel.text = [NSString stringWithFormat:@"%1.1f",aBook.distance];


    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;


    return cell;
}

1 Ответ

1 голос
/ 05 сентября 2011

Проблема кроется в файле xib и в том, как он загружается.Чтобы исключить проблему и получить полный контроль, следующий код был заменен версией IB пользовательской ячейки.

    static NSString *CellTableIdentifier = @"CellTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellTableIdentifier] autorelease];
    CGRect nameLabelRect = CGRectMake(15, 5, 200, 15);
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:nameLabelRect];
    nameLabel.textAlignment = UITextAlignmentLeft;
    nameLabel.font = [UIFont boldSystemFontOfSize:14];
    nameLabel.tag = kNameTag;
    [cell.contentView addSubview:nameLabel];
    [nameLabel release];

    CGRect catagoryLabelRect = CGRectMake(15, 26, 100, 15);
    UILabel *catagoryLabel = [[UILabel alloc]initWithFrame:catagoryLabelRect];
    catagoryLabel.textAlignment = UITextAlignmentLeft;
    catagoryLabel.font = [UIFont systemFontOfSize:12];
    catagoryLabel.tag = kExperienceTag;
    [cell.contentView addSubview:catagoryLabel];
    [catagoryLabel  release];

    CGRect distanceLabelRect = CGRectMake(210, 26, 70, 15);
    UILabel *distanceLabel = [[UILabel alloc] initWithFrame:distanceLabelRect];
    distanceLabel.textAlignment = UITextAlignmentRight;
    distanceLabel.font = [UIFont boldSystemFontOfSize:12];
    distanceLabel.tag = kDistanceTag;
    [cell.contentView addSubview:distanceLabel];
    [distanceLabel release];
}

Спасибо за помощь в продумывании этого.Теперь прокрутка работает отлично.

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