首页 文章

使用RootController的多视图TableView

提问于
浏览
0

我正在尝试使用导航模板创建一个多视图应用程序 . 我希望 table 位于初始视图的底部,顶部有其他视图(图像视图,标签等) .

最初我修改了RootViewController.xib以调整tableview的大小,并添加了图像视图,但除了全尺寸表外没有显示任何内容 .

所以我修改了RootViewController.xib来添加UIView,然后在该视图中添加了一个UITableView和UIImageView . 我为tableView添加了IBOutlet . 在我的RootViewController.xib中,File'S Owner(RootViewController)与视图和表视图有出口连接 . 当我右键单击UITableView时,我看到文件所有者的引用插座 . 当我右键单击UIView时,我看到文件所有者的引用插座 . (我没有UIImageView的出口,因为我不修改代码;我需要一个?) .

但是,当我启动应用程序时,它会崩溃并显示以下消息:

'NSInternalInconsistencyException',原因:' - [UITableViewController loadView]加载了“RootViewController”笔尖,但没有得到UITableView .

这是cellForRowAtIndexPath方法:

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

    static NSString *QuizIdentifier = @"QuizIdentifier";

    UITableViewCell *cell =
    [tableView dequeueReusableCellWithIdentifier:QuizIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc]
                 initWithStyle:UITableViewCellStyleDefault
                 reuseIdentifier:QuizIdentifier]
                autorelease];
    }

    // Configure the cell.

    UIImage *_image = [UIImage imageNamed:@"icon"];
    cell.imageView.image = _image;

    NSInteger row = [indexPath row];
    cell.textLabel.text = [_categories objectAtIndex:row];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

1 回答

  • 0

    你得到了这个错误,因为当你创建一个基于导航的iOS应用程序时,Xcode使RootViewController成为 UITableViewController 的子类 . 将 UITableView 与正常 UIView 组合后,您的子类不再符合 UITableViewController 类 . 因此,在RootViewController.m和.h文件中将 UITableViewController 更改为 UIViewController . 您可能还需要在 init 方法中更改其他一些内容,但让我们从这开始 .

    试试这个:

    - (UITableViewCell *)tableView:(UITableView *)tableView 
             cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        NSLog(@"\n\nI'm in Root / cellForRowAtIndexPath");
    
        static NSString *QuizIdentifier = @"QuizIdentifier";
    
        UITableViewCell *cell = 
        [tableView dequeueReusableCellWithIdentifier:QuizIdentifier];
    
        if (cell == nil) {
            cell = [[[UITableViewCell alloc]
                     initWithStyle:UITableViewCellStyleDefault
                     reuseIdentifier:QuizIdentifier]
                    autorelease];
            UIImage *_image = [UIImage imageNamed:@"icon"];
            cell.imageView.image = _image;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    
        // Configure the cell.
        NSInteger row = [indexPath row];
        cell.textLabel.text = [_categories objectAtIndex:row];
    
        NSLog(@"  We are at row %i with label %@ ", row, cell.textLabel.text);
    
        return cell;
    }
    

相关问题