Настройка UITableViewCell - PullRequest
       5

Настройка UITableViewCell

3 голосов
/ 16 июня 2011

Я пытаюсь настроить UITableViewCell для "прикраски" приложения, и у меня возникают проблемы. Мой код:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UIImage *rowBackground;
UIImage *selectionBackground;
NSInteger sectionRows = [tableView numberOfRowsInSection:[indexPath section]];
NSInteger row = [indexPath row];

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if ( cell == nil ) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

if (row == 0 && row == sectionRows - 1) {
    rowBackground = [UIImage imageNamed:@"topAndBottomRow.png"];
    selectionBackground = [UIImage imageNamed:@"topAndBottomRowSelected.png"];
    NSLog(@"topAndBottom");
}
else if ( row == 0 ) {
    rowBackground = [UIImage imageNamed:@"topRow.png"];
    selectionBackground = [UIImage imageNamed:@"topRowSelected.png"];
    NSLog(@"topRow");
}
else if ( row == sectionRows - 1 ) {
    rowBackground = [UIImage imageNamed:@"bottomRow.png"];
    selectionBackground = [UIImage imageNamed:@"bottomRowSelected.png"];
    NSLog(@"bottomRow");
} else {
    rowBackground = [UIImage imageNamed:@"middleRow.png"];
    selectionBackground = [UIImage imageNamed:@"middleRowSelected.png"];
    NSLog(@"middleRow");
}

((UIImageView *)cell.backgroundView).image = rowBackground;
((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;

NSString *cellValue = [[listOfItems objectAtIndex:indexPath.row] description];
cell.textLabel.text = cellValue;

return cell;                 
}

Он выбирает соответствующий образ, как показано в журнале консоли, но у меня есть две проблемы: 1. Установка (cell.backgroundView) .image не имеет никакого эффекта 2. Установка ошибок (cell.selectedBackgroundView) .image с помощью: - [UITableViewCellSelectedBackground setImage:]: нераспознанный селектор, отправленный экземпляру 0x4b90670

Я могу найти подсказки о проблемах UIView с этим в Google, но не нашел ничего о UITableViewCell. Помощь

1 Ответ

4 голосов
/ 16 июня 2011

Есть некоторые несовместимости - вы не можете установить официальное фоновое изображение, IIRC, есть только фоновый UIView. Если вы хотите что-то, что является UIImageView, вы должны сделать это сами.

Таким образом, вместо этого вам нужно вручную установить backgroundView ячейки как пустой UIImageView * при создании ячейки, например ::

cell.backgroundView = [[[UIImageView alloc] init] autorelease];

... которые вы можете затем установить изображения.

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