2013-03-05 2 views
0

У меня есть приложение, в котором я меняю цвет выбранной ячейки, используя cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_gray_with_line.png"]];. Но когда я делаю это после выбора в didselect это не меняет свой фон назад к предыдущему виду, после нажатияКак изменить цвет фона выбранной ячейки после нажатия?

if (indexPath != nil) { 
    [tableView deselectRowAtIndexPath:indexPath animated:YES];  
} 

мне нужна такой же фон, как обычно, когда выскакивают обратно в этом виде контроллер, без перезагрузки табличный вид. Кто-нибудь может мне помочь?

ответ

1

Вы должны установить два свойства для вашего UITableViewCell.

  1. backgroundView
  2. selectedBackgroundView

Кроме того, вы должны установить cell.selectionStyle в UITableViewCellSelectionStyleNone.

+0

я это делаю бушель BackgroundColor для некоторых reasons.that проблема здесь – hacker

0

Это следует сделать,

- (void)viewDidAppear:(BOOL)animated 
{ 
[super viewDidAppear:animated]; 
[self.tableView cellForRowAtIndexPath:[self.tableView indexPathForSelectedRow] ].backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"normal.png"]]; 
} 
0
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    //First you get the cell you want to change 
    UITableViewCell* theCell = [tableView cellForRowAtIndexPath:indexPath]; 

    //Then you change the properties (label, text, color etc..) in your case, the background color 
    theCell.contentView.backgroundColor=[UIColor redColor]; 

    //Deselect the cell so you can see the color change 

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

} 
1

UITableViewControllers будет, если их имущество clearsSelectionOnViewWillAppear установлен в YES, снимите выбранную строку на viewWillAppear. Если вы не используете контроллер представления таблицы с этой установкой (по умолчанию Да, я думаю), сделайте это сами:

- (void)viewWillAppear:(BOOL)animated { 

    [super viewWillAppear:animated]; 

    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

Реализовать сделал (или будет) отменить метод делегата и исправить цвет:

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    // do what you want to the cell 
} 

Кстати, другие на SO рекомендуется устанавливать цвет на просмотр содержимого ячейки (не cell.backgroundColor, но cell.contentView.backgroundColor), и делает это в willDisplayCell:forRowAtIndexPath:.

+0

я это сделал, но не решается. Я не мог перезагрузить tableview. – hacker

0

такой же ответ, как @dand, но немного измените.

- (void) viewWillAppear: (BOOL) animated 
{ 
    [super viewWillAppear: animated]; 

    NSIndexPath *indexPath = [self.tableview indexPathForSelectedRow]; 
    [self.tableview deselectRowAtIndexPath: indexPath animated: YES]; 
} 

и изменить этот метод в пользовательской реализации ячеек.

- (void) setSelected: (BOOL) selected animated: (BOOL) animated 
{ 
    [super setSelected: selected animated: animated]; 
    // Configure the view for the selected state 

    if (selected) 
    { 
     self.backgroundColor = [UIColor redColor]; 
    } 
    else 
    { 
     self.backgroundColor = [UIColor clearColor]; 
    } 
} 
Смежные вопросы