【问题标题】:CustomCell setImageViewCustomCell setImageView
【发布时间】: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


【解决方案1】:

我可能错了,但我认为 imageView 属性已经在 UITableViewCell 上实现,并且它是只读属性。 在子类上重新实现它可能会产生冲突。

尝试更改您的 imageView 属性的名称,如果有效,请告诉我。

【讨论】:

    【解决方案2】:

    转到 NewsCell.h,您是否在 imageView 上看到警告?问题是您试图用 readwrite 属性 替换 readonly 属性。但它不允许你这样做。

    你想要做你目前正在做的事情,你可以尝试声明一个不同的 imageView 像:-

    NewsCell.h

    @interface NewsCell : UITableViewCell
    
    @property (nonatomic, strong) UIImageView* imageView2;
    @property (nonatomic, strong) UILabel* titleLabel;
    
    @end
    

    NewsCell.m

    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 44, 40)];
        [self.contentView addSubview:self.imageView2];
        self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 5, 200, 25)];
        self.titleLabel.font = [UIFont systemFontOfSize:24];
        [self.contentView addSubview:self.titleLabel];
    }
    return self;
    }
    

    对于 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.imageView2.image = [UIImage imageNamed:@"abcde.jpg"];
                break;
        }
    }
    return cell;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-23
      • 1970-01-01
      • 1970-01-01
      • 2016-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多