我想在选择单元格时更改单元格的背景图像。
我尝试使用以下语法:
- (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"];
}
这会更改前景中的图像,而不是背景中的图像。
请先帮助并表示感谢。
最佳答案
您可以使用cellForRowAtIndexPath
方法设置UITableviewCell的属性 setSelectedBackgroundView 。
喜欢 ,
- (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这样的问题。
关于ios - 如何更改didSelectRowAtIndexPath中单元格的背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20949100/