Iphone 自定义UITableViewCell未加载
我正在使用interface builder创建自己的自定义UITableViewCell。我支持iOS 5和iOS 6,但我不想使用故事板。请不要建议故事板。我坚持使用界面生成器,并以编程方式编写 我创建了一个子类UITableViewCell的类。以下是.h文件:Iphone 自定义UITableViewCell未加载,iphone,objective-c,ios,uitableview,Iphone,Objective C,Ios,Uitableview,我正在使用interface builder创建自己的自定义UITableViewCell。我支持iOS 5和iOS 6,但我不想使用故事板。请不要建议故事板。我坚持使用界面生成器,并以编程方式编写 我创建了一个子类UITableViewCell的类。以下是.h文件: @interface CategoryCell : UITableViewCell { __weak IBOutlet UIImageView *image; __weak IBOutlet UILabel *na
@interface CategoryCell : UITableViewCell
{
__weak IBOutlet UIImageView *image;
__weak IBOutlet UILabel *name;
__weak IBOutlet UILabel *distance;
__weak IBOutlet UILabel *number;
__weak IBOutlet UIImageView *rating;
}
@property (nonatomic, weak) IBOutlet UIImageView *image;
@property (nonatomic, weak) IBOutlet UILabel *name;
@property (nonatomic, weak) IBOutlet UILabel *distance;
@property (nonatomic, weak) IBOutlet UILabel *number;
@property (nonatomic, weak) IBOutlet UIImageView *rating;
@end
XIB文件的类型为UIViewController,视图的类型为CategoryCell。我把插座接上了
问题
dequeueResableCellWithIdentifier
未调用自定义单元格。以下是我所拥有的:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"CategoryCell";
CategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CategoryCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
.....
}
return cell
}
当我将loadBundle
行替换为:cell=[[CategoryCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]代码>,它确实有效。但是笔尖不加载。所以一个单元格加载,但不是我自己的单元格,所以我无法设置我想要的标签和图像。添加常规load bundle行(如上面的示例所示)并中断自定义单元格的init
方法时,不会调用它。此外,我得到的是一个全白色屏幕,它覆盖了模拟器中整个iPhone屏幕
为什么会这样?我做错了什么?当我尝试将插座设置为strong
(我知道我不应该这么做)时,它也不起作用
编辑:
我将NSBundle行替换为:
UIViewController *temporaryController = [[UIViewController alloc] initWithNibName:@"CategoryCell" bundle:nil];
cell = (CategoryCell *)temporaryController.view;
我在NSBundle方法中犯了什么错误?这应该是一种“更容易”的方法
XIB文件的类型为UIViewController,视图的类型为CategoryCell。我把插座接上了
问题在于,XIB的根对象必须是UITableViewCell或其子类。
并确保在XIB中设置了重用标识符
XIB文件的类型为UIViewController,视图的类型为CategoryCell。我把插座接上了
问题在于,XIB的根对象必须是UITableViewCell或其子类。
并确保在XIB中设置了重用标识符 您是否尝试过tableview中的RegisterInB方法?从iOS5开始加载nib非常方便
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"CategoryCell" bundle:nil]
forCellReuseIdentifier:@"CategoryCell"];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"CategoryCell";
CategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
return cell
}
确保您在CategoryCell.nib中有define identifier,它位于属性检查器下!希望这能奏效。您有没有尝试过tableview中的RegisterInB方法?从iOS5开始加载nib非常方便
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"CategoryCell" bundle:nil]
forCellReuseIdentifier:@"CategoryCell"];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"CategoryCell";
CategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
return cell
}
确保您在CategoryCell.nib中有define identifier,它位于属性检查器下!希望这能奏效。您是否设置了断点并查看了topLevelObjects的值,然后查看了单元格?这将是有用的信息,可以查看它们是否为零。您是否设置了断点并查看了topLevelObjects的值,然后查看了单元格?这将是有用的信息,以查看它们是否为零。它属于CategoryCell类型。文件的所有者是UIViewController,但视图的类型是CategoryCell,它是UITableViewCell的子类。我还设置了重用标识符。它是CategoryCell类型。文件的所有者是UIViewController,但视图的类型是CategoryCell,它是UITableViewCell的子类。我还设置了重用标识符。你检查过nib中的类是指向CategoryCell的吗?不是文件的所有者,而是nib中对象下的单元格。是的,它是CategoryCelln类型,此xib的文件所有者应该是NSObject而不是UIViewController,因为如果以这种方式实现,则不需要将nib附加到控制器。是否检查了nib中的类是否指向CategoryCell?不是文件的所有者,而是nib中对象下的单元格。是的,我这样做了,它属于CategoryCellan类型,此xib的文件所有者应该是NSObject而不是UIViewController,因为如果以这种方式实现,则不需要将nib附加到控制器。