【发布时间】:2014-05-22 16:33:20
【问题描述】:
我制作了自定义 UITableViewCell 并将 UIImageView 添加到单元格中。
然后,当我构建时,应用程序异常终止并显示以下日志消息。
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NewsCell setImageView:]: unrecognized selector sent to instance 0x109139f80'
我有这个代码。
ViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[NewsCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (indexPath.section == 0) {
switch (indexPath.row) {
case 0:
cell.imageView.image = [UIImage imageNamed:@"abcde.jpg"];
break;
}
}
return cell;
}
NewsCell.h
@interface NewsCell : UITableViewCell
@property (nonatomic, strong) UIImageView* imageView;
@property (nonatomic, strong) UILabel* titleLabel;
@end
NewsCell.m
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 44, 40)];
[self.contentView addSubview:self.imageView];
self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 5, 200, 25)];
self.titleLabel.font = [UIFont systemFontOfSize:24];
[self.contentView addSubview:self.titleLabel];
}
return self;
}
如何修复它以将自定义单元格反映到 tableView?
【问题讨论】:
-
尝试将 ImageView 添加到单元格的 contentView 中,而不是直接添加到单元格中
-
你能把 NSLog(@"%@",NSStringFromClass([cell class])) 的输出粘贴到这里吗
标签: ios objective-c uitableview uiimageview