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

标签 ios iphone ipad uitableview uiimageview

我想在选择单元格时更改单元格的背景图像。

我尝试使用以下语法:

- (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/

相关文章:

ios - iOS 13:如何检测“语音控制”是否正在运行

iphone - mobileconfig 文件和 safari

ios - 适用于 iPhone 4S 和 5 的 AVCaptureSessionPresetHigh resolution

android - 平板电脑(iPad/Android)-服务器通讯协议(protocol)

ios - UIBarButtonItem 色调颜色不起作用

ios - UIApplication 共享对象未提供预定通知

iphone - 如何通过单击 TabbarItem 按钮来获取 Tabbar Item 按钮的 Click 事件?

iphone - 嵌套 UIScrollView - 滚动方向

iphone - 如何将我的 3ds Max 模型放入 iPhone 3D 游戏中?

javascript - 在 iOS Cordova 框架中包含我的 custom.js