在cellForRowAtIndexPath方法中设置自定义单元格的正确方法?

时间:2014-01-26 01:35:16

标签: ios objective-c uitableview ios7

男人,我不得不继续回顾我的旧xcode项目,以了解如何在我的cellForRowAtIndexPath委托方法中设置自定义单元格,这是因为我看到的方法的大小。 / p>

在不使用故事板的情况下,使用cellForRowAtIndexPath方法设置自定义单元格的正确代码是什么?

我目前正在做这样的事情:

//This works for me btw
static NSString *simpleTableIdentifier = @"customcell";
CustomTableViewCell *customCell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (customCell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:self options:nil];
    customCell = [nib objectAtIndex:0];
}

然后我看到其他人使用loadWithNibName和其他人使用registerWithNib方法在viewDidLoad方法中注册他们的笔尖。

在委托方法中设置自定义单元格的正确方法是什么?

我最近也被告知,当我处理自定义单元格时,我不需要调用dequeueReusable..方法。有人可以澄清一下为您设置cellForRowAtIndexPath委托方法的正确方法。没有使用故事板。我已正确设置自定义单元格视图及其各自的.m.h文件。

1 个答案:

答案 0 :(得分:1)

如果是CustomTableViewCell nib文件存在于同一个UITableViewController nib中,你的代码将正常工作。否则,您需要在cellForRowAtIndexPath中加载nib文件:

if(cell==nil){
NSArray * nibContents = [[NSBundle mainBundle] loadNibNamed:"CustomTableViewCell" owner:self options:NULL];

NSEnumerator * nibEnumerator = [nibContents objectEnumerator];
NSObject * nibItem = nil;

while ((nibItem = [nibEnumerator nextObject]) != nil) {

    if ([nibItem isKindOfClass:[UITableViewCell class]]) {
        cell=(CustomTableViewCell*)nibItem;
    }

}

}
相关问题