使用loadNIB加载UITableViewCell时,如何将自定义子视图添加到UITableViewCell派生类

时间:2010-03-07 01:30:57

标签: iphone cocoa-touch uitableview

我正在尝试将自定义视图控件添加到我在界面构建器中设计的自定义UITableViewCell。

现在,要加载UITableCellView,我正在使用

NSArray * loadedViews = [[NSBundle mainBundle] loadNibNamed:@“CustomSearchResultsCell”owner:self options:nil];

将调用CustomSearchResultsCell类的哪个方法Nib加载视图并对其进行初始化。我尝试使用viewDidLoad,但UITableViewCell不响应此方法。在这种情况下,也没有调用initWithStyle。

TIA 尼丁

2 个答案:

答案 0 :(得分:3)

从nib加载的视图使用initWithCoder:进行初始化,您可以采用与initWithFrame:类似的方式实现。

答案 1 :(得分:0)

另外,请查看名为AdvancedTableViewCells的Apple示例代码。在此示例中,他们使用以下代码从XIB加载表视图单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ApplicationCell";

    ApplicationCell *cell = (ApplicationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        [[NSBundle mainBundle] loadNibNamed:@"IndividualSubviewsBasedApplicationCell" owner:self options:nil];
        cell = tmpCell;
        self.tmpCell = nil;     
    }

    return cell;
}