2014-01-06 3 views
1

Я хочу изменить фоновое изображение ячейки, когда ячейка выбрана.Как изменить фоновое изображение ячейки в didSelectRowAtIndexPath

Я попытался с этим синтаксисом:

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{ 
//for adding image 
cell.ImgCateg.image=[UIImage imageNamed:[ImgArr objectAtIndex:indexPath.row]]; 
    ... 
} 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CRHomeCategCell *cell = (CRHomeCategCell*)[_tblCateg cellForRowAtIndexPath:indexPath]; 
    cell.ImgCateg.image = [UIImage imageNamed:@"DefaultBg.png"]; 
} 

это меняет изображение на переднем плане не в фоновом режиме.

Пожалуйста, помогите и благодарите заранее.

ответ

2

Вы можете установить UITableViewCell в собственность setSelectedBackgroundView в cellForRowAtIndexPath методом.

как,

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{ 
{ 
.... 
UIImageView *normalCellBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)]; 

[normalCellBG setImage:[UIImage imageNamed:@"box_nonselected.png"]];//Set Image for Normal 
[normalCellBG setBackgroundColor:[UIColor clearColor]]; 
[cell setBackgroundView:normalCellBG]; 


UIImageView *selecetedCellBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)]; 
[selecetedCellBG setImage:[UIImage imageNamed:@"box_selected.png"]];//Set Name for selected Cell 
[selecetedCellBG setBackgroundColor:[UIColor clearColor]]; 
[cell setSelectedBackgroundView:selecetedCellBG ]; 

} 

взгляд this SO вопрос.

+0

Вашего ответ работает хорошо, но этот метод вызывался много раз, если мы пишем в 'cellForAtIndexPath' и изображения получать переопределены. как вызвать этот метод только один раз? – Krunal

+0

Это простой код для 'setSelectedBackgroundView' ячейки, вам нужно написать правильный код создания ячейки, например' yourcell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier]; если (ячейки == ноль) {// создаем новый} ' –

+0

я это написал, но не помогает, ' CRHomeCategCell * ячейка = (CRHomeCategCell *) [_ tblCateg dequeueReusableCellWithIdentifier: CellIdentifier], если (ячейка == ноль) {NSArray * nib = [[NSBundle mainBundle] loadNibNamed: @ "CRHomeCategCell" владелец: self options: nil]; для (id oneObject in nib) если ([oneObject isKindOfClass: [CRHomeCategCell класс]]) cell = (CRHomeCategCell *) oneObject; [self setCellBGColorNormalAndSelectedForTableViewCell: cell cellForRowAtIndexPath: indexPath]; } cell.ImgCateg.image = [UIImage imageNamed: [ImgArr objectAtIndex: indexPath.row]]; return cell; ' – Krunal

1

Вы можете сделать это следующим образом:

UIImage *cellImage = [UIImage imageNamed:@"myimage.png"]; 
UIImageView *cellView = [[UIImageView alloc] initWithImage:cellImage]; 
cellView.contentMode = UIContentViewModeScaleToFill; 
cell.backgroundView = cellView; 
Смежные вопросы