简体   繁体   English

如何在didSelectRowAtIndexPath中更改单元格的背景图像

[英]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 ];

}

look this SO question. 看看这个问题。

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.

相关问题 如何根据警报视图选择从didselectrowatindexpath更改单元格 - how to change cell from didselectrowatindexpath based on alertview selection 在didSelectRowAtIndexPath中更改自定义单元格的标签高度 - Change label height of custom cell within didSelectRowAtIndexPath 弹出视图控制器时如何更改单元格的背景图像? - how to change the background image of the cell when poping back to the viewcontroller? 如何从didSelectRowAtindexPath:function访问cell imageView - How to Access cell imageView from didSelectRowAtindexPath: function 如何在 didSelectRowAtIndexPath 中到达当前选定的单元格? - How to reach the current selected cell in didSelectRowAtIndexPath? UICollectionView带图像的单元格,在Swift中单击更改背景 - UICollectionView Cell with Image, change Background with click in Swift 带图像的UICollectionView单元格,单击更改背景 - UICollectionView Cell with Image, change Background with click iPhone旋转时更改单元格中的背景图像 - change background image in a cell when iPhone rotates 滚动时CollectionView更改单元格背景图像 - collectionview change cell background image when scrolling 更改didSelectRowAtIndexPath上单元格的顶部边框宽度和颜色的最佳方法是什么? - What is the best way to change top border width and color of cell at didSelectRowAtIndexPath?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM