我有一个简单的UITableView。在iOS 7上一切正常,但在iOS 8上没有。基本上我有一个自定义UITableViewCell,其中包含文本标签,按钮,图像。当我按下tableview单元格内的按钮时,我收到了错误。这是代码:
-(IBAction)addToFavorites:(id)sender {
UIButton *editButton = (UIButton*)sender;
GetMoreTableViewCell *editCell = (GetMoreTableViewCell*)[[[editButton superview] superview] superview];
int editIndex = (int)[[_chapters indexPathForCell:editCell] row];
[self readTopScorePlist];
if([[[[_data valueForKey:key] valueForKey:[NSString stringWithFormat:@"%i",editIndex]] valueForKey:@"isDefault"] boolValue] == YES) {
[editCell.addButton setImage:[UIImage imageNamed:@"fav_0.png"] forState:UIControlStateNormal];
[self saveToPlist:NO sender:sender];
NSLog(@"NEW %@",[[[_data valueForKey:key] valueForKey:[NSString stringWithFormat:@"%i",editIndex]] valueForKey:@"isDefault"]);
}
else {
[editCell.addButton setImage:[UIImage imageNamed:@"fav_1.png"] forState:UIControlStateNormal];
[self saveToPlist:YES sender:sender];
NSLog(@"NEW %@",[[[_data valueForKey:key] valueForKey:[NSString stringWithFormat:@"%i",editIndex]] valueForKey:@"isDefault"]);
}
}
该方法被调用但是我在这里得到例外:[editCell.addButton setImage:[UIImage imageNamed:@"fav_0.png"] forState:UIControlStateNormal];
我得到的期望是:
2014-09-22 10:08:28.223 HandyHoch[657:252982] -[UITableViewWrapperView addButton]: unrecognized selector sent to instance 0x17e9fd80
我猜测无法找到addButton,但我应该在iOS 8中做到这一点?提前谢谢。
The UITableViewCell custom class:
@implementation GetMoreTableViewCell
@synthesize ttitle, cellImage, cellBackground, changeButton, deleteButton,addButton;
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if(self) {
ttitle.textColor = [UIColor whiteColor];
ttitle.numberOfLines = 30;
ttitle.lineBreakMode = NSLineBreakByCharWrapping;
ttitle.textAlignment = NSTextAlignmentCenter;
[self.contentView addSubview:ttitle];
[self.contentView addSubview:addButton];
[self setAutoresizesSubviews:YES];
}
return self;
}
@end
答案 0 :(得分:0)
似乎在iOS 8中视图层次结构发生了变化,因此(GetMoreTableViewCell*)[[[editButton superview] superview] superview];
会返回不同的视图。最简单的解决方案是更改iOS 8的超级视图调用次数。更可靠的解决方案是将目标连接到GetMoreTableViewCell
。