【问题标题】:Custom table cell view with xib throwing exception on ios 8ios 8上带有xib抛出异常的自定义表格单元格视图
【发布时间】:2016-02-15 21:07:20
【问题描述】:

我在创建自定义表格单元格视图时遇到问题。

CustomCell.h

#import <UIKit/UIKit.h>

@interface CustomCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UILabel *date;
@property (weak, nonatomic) IBOutlet UILabel *title;
@property (weak, nonatomic) IBOutlet UILabel *content;

@end

CustomCell.m

#import "CustomCell.h"

@implementation CustomCell
@synthesize title, imageView, content, date;
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

表视图控制器

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *simpleTableIdentifier = @"CustomCell";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:simpleTableIdentifier owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    cell.title.text = [titles objectAtIndex:indexPath.row];
    cell.imageView.image = [UIImage imageNamed:[images objectAtIndex:indexPath.row]];
    cell.content.text = [shortTexts objectAtIndex:indexPath.row];
    cell.date.text = [dates objectAtIndex:indexPath.row];
    return cell;
}

并抛出异常

2014-10-14 19:06:59.290 pl.wroclaw.2017[7450:2962005] -[CustomCell _needsSetup]: unrecognized selector sent to instance 0x127d15bc0
2014-10-14 19:06:59.291 pl.wroclaw.2017[7450:2962005] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CustomCell _needsSetup]: unrecognized selector sent to instance 0x127d15bc0'
*** First throw call stack:
(0x1871a2084 0x1977d80e4 0x1871a9094 0x1871a5e48 0x1870ab08c 0x18ba63c08 0x18bc1a3bc 0x18bc0efc4 0x18ba04c60 0x18b921874 0x18b279d58 0x18b274944 0x18b2747e8 0x18b273fe8 0x18b273d6c 0x18bbae7f0 0x18bbaf69c 0x18bbad820 0x18f3a5640 0x18715a360 0x187159468 0x187157668 0x187085664 0x18b98f500 0x18b98a4f8 0x1000b65a4 0x197e46a08)
libc++abi.dylib: terminating with uncaught exception of type NSException

我尝试了一切,从删除 xib 并再次添加它。我用我以前的项目仔细检查了我的方法,它确实有效。现在我的新项目没有,我似乎找不到任何解决方案。我尝试查找有关此“_needsSetup”的一些信息,但找不到任何信息。

感谢您的帮助。

【问题讨论】:

    标签: ios objective-c iphone ios8


    【解决方案1】:

    我有同样的错误,这是因为我忘记在这个方法中返回一个单元格:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    

    【讨论】:

      【解决方案2】:

      我不知道这是否有帮助,但推荐使用基于 xib 的单元格的方法是注册 nib,而不是在 cellForRowAtIndexPath: 中加载 nib。所以,在viewDidLoad中,放,

      [self.tableView registerNib:[UINib nibWithNibName:<"your nib name here"> bundle:nil] forCellReuseIdentifier:@"CustomCell"];
      

      删除 if(cell==nil) 子句。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-13
        相关资源
        最近更新 更多