访问,从单个笔尖引用多个视图

时间:2012-08-31 10:41:43

标签: iphone ios xib nib

在表格视图中,我正在插入具有重用标识符的单元格。所以,我必须为每个单元格创建一个nib(xib)文件。我想将所有单元格视图放在一个xib文件中并单独引用它们。怎么做?

3 个答案:

答案 0 :(得分:1)

您可以将xib作为视图数组访问,如下所示:

-(UITableViewCell *) viewCellForSection:(NSInteger) section
{
    UITableViewCell *view = nil;

    NSArray* views= [[NSBundle mainBundle] loadNibNamed:@"myXib" owner:self options:nil];

    switch ( section) {
        case 0: 
            view = (UITableViewCell*) [views objectAtIndex:1];
            break;
        case 1: 
            view = (UITableViewCell*) [views objectAtIndex:0];
            break;
        default: 
            view = (UITableViewCell*) [views objectAtIndex:2];
    }
    return view;
}

答案 1 :(得分:0)

您必须在方法(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath中实现代码。在表中选择单元格时,将调用此方法。这就是你必须实现你想要的东西的原因。

我希望以下代码对您有所帮助。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

// yourObject is object of yourViewController and you can declare it .h file.

                    if (!yourObject) {
                    yourObject = [[yourViewController alloc] initWithNibName:@"yourViewController" bundle:nil];

                }

                UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
                self.navigationItem.backBarButtonItem = backBarButtonItem;
                [backBarButtonItem release];
                [self.navigationController pushViewController:yourObject animated:YES];

    }

答案 2 :(得分:0)

我知道这是一个旧线程,但当我尝试在UIT中为UITableViewCells提供多个视图时,我实际上在日志中找到了响应。这是日志:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'invalid nib registered for identifier (cellIdentifier) - nib must
contain exactly one top level object which must be a UITableViewCell instance'
相关问题