[英]how to change the background image of the cell in didSelectRowAtIndexPath
I want to change the background image of the cell when cell it is selected. 我想在选择单元格时更改单元格的背景图像。
I have tried with this syntax: 我尝试过这种语法:
- (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"];
}
this changes image in the foreground not in the background. 这会改变前景中的图像而不是背景中的图像。
Please help and thanks in advance. 请提前帮助和感谢。
You can set UITableviewCell's property setSelectedBackgroundView in cellForRowAtIndexPath
method. 您可以在cellForRowAtIndexPath
方法中设置UITableviewCell的属性setSelectedBackgroundView 。
like , 喜欢 ,
- (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 ];
}
You can do this on following way: 您可以通过以下方式执行此操作:
UIImage *cellImage = [UIImage imageNamed:@"myimage.png"];
UIImageView *cellView = [[UIImageView alloc] initWithImage:cellImage];
cellView.contentMode = UIContentViewModeScaleToFill;
cell.backgroundView = cellView;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.