Настройка UITableViewCell - изменить фон - PullRequest
0 голосов
/ 19 сентября 2011

Я уверен, что это вопрос, который задавали и отвечали, но я не могу найти то, что мне нужно.Я пытаюсь настроить фон моего UITableView, поэтому для этого я создал подкласс UITableViewCell и создал собственную ячейку с помощью IB.Я успешно подключил пользовательскую ячейку к табличному представлению, поэтому полученная ячейка отображается.Я пытаюсь изменить фон каждой ячейки, вот что я использую:

if(row == 1){
    rowBackground = [UIImage imageNamed:@"VehicleExpenses_button.png"];
    selectionBackground = [UIImage imageNamed:@"VehicleExpenses_button_selected.png"];
    ((UIImageView *)cell.backgroundView).image = rowBackground;
  //  ((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;
}

в методе

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

моего класса Controller.Я проверил с помощью отладчика, и все строки в коде стали называться ...

Есть идеи?

Это весь метод:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"HomeViewCell";
    static NSString *CellNib = @"HomeViewCell";

    UIImage *rowBackground;
    UIImage *selectionBackground;

    NSInteger row = [indexPath row];


    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
        cell = (UITableViewCell *)[nib objectAtIndex:0];
    }

    if(row == 1){

        rowBackground = [UIImage imageNamed:@"VehicleExpenses_button.png"];
        selectionBackground = [UIImage imageNamed:@"VehicleExpenses_button_selected.png"];
        ((UIImageView *)cell.backgroundView).image = rowBackground;
      //  ((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;
    }

    // perform additional custom work...

    return cell;
}

Ответы [ 3 ]

2 голосов
/ 19 сентября 2011

Может быть, я ошибаюсь, но я не вижу, где вы инициализировали backgroundView как UIImageView.Вот что я хотел бы добавить к строкам, в которых вы создаете ячейки:

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
        cell = (UITableViewCell *)[nib objectAtIndex:0];
        cell.backgroundView = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, tableView.bounds.size.width, cellHeight)] autorelease];
        cell.selectedBackgroundView = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, tableView.bounds.size.width, cellHeight)] autorelease];
    }

Примечание: замените "cellHeight" на желаемую высоту.

Надеюсь, это поможет.

0 голосов
/ 19 сентября 2011

Небольшая проблема. Вместо приведения и установки свойства изображения backgroundView и selectedBackgroundView установите для них значение UIImageView, созданное на основе ваших изображений.

cell.backgroundView = [[[UIImageView alloc] initWithImage:rowBackground] autorelease];
cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:selectionBackground] autorelease];
0 голосов
/ 19 сентября 2011

Вы можете использовать это:

UIImageView *cellBackGround = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"selectedRow.png"]];
[[tableView cellForRowAtIndexPath:indexPath] setBackgroundView:cellBackGround];
[cellBackGround release];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...