Ios 带有UIButton的自定义UITableViewCell:无法更改UIImage
我有一个自定义的Ios 带有UIButton的自定义UITableViewCell:无法更改UIImage,ios,iphone,uitableview,uibutton,Ios,Iphone,Uitableview,Uibutton,我有一个自定义的UITableView和自定义的UITableViewCell它包含一个UIButton当用户选择它时,我想更改所选按钮的背景图像 不幸的是,每当我按下图像时,它都不会改变状态,并显示: 由于未捕获异常而终止应用程序 “NSInvalidArgumentException”,原因:'-[UIView setBackgroundImage:forState:: 这是我的代码片段: 我的日志正确显示所选按钮的值,但无法更改按钮的图像 谢谢阅读。检查按钮的标签==0。这是UITable
UITableView
和自定义的UITableViewCell
它包含一个UIButton
当用户选择它时,我想更改所选按钮的背景图像
不幸的是,每当我按下图像时,它都不会改变状态,并显示:
由于未捕获异常而终止应用程序
“NSInvalidArgumentException”,原因:'-[UIView
setBackgroundImage:forState::
这是我的代码片段:
我的日志正确显示所选按钮的值,但无法更改按钮的图像
谢谢阅读。检查按钮的标签==0。这是UITableViewCell的contentView的默认标记,您可能正在尝试更改单元格contentView的图像。尝试将您的
AddToFav:
方法修改为:
-(IBAction)AddToFav:(UIButton *)sender{
NSLog(@"Value of selected button = %ld",(long)[sender tag]);
[sender setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
[sender setBackgroundImage:[UIImage imageNamed:@"grey-star.png"] forState:UIControlStateNormal];
}
或者使用以下命令:
-(IBAction)AddToFav:(UIButton *)sender{
NSLog(@"Value of selected button = %ld",(long)[sender tag]);
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
for (UIButton *btn in [cell.contentView.superview subviews] ) {
if ([btn isKindOfClass:[UIButton class]]&& btn.tag ==(long)[sender tag]) {
[btn setBackgroundImage:[UIImage imageNamed:@"grey-star.png"]
}
}
}
-(IBAction)AddToFav:(UIButton *)sender{
NSLog(@"Value of selected button = %ld",(long)[sender tag]);
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
for (UIButton *btn in [cell.contentView.superview subviews] ) {
if ([btn isKindOfClass:[UIButton class]]&& btn.tag ==(long)[sender tag]) {
[btn setBackgroundImage:[UIImage imageNamed:@"grey-star.png"]
}
}
}