Objective c 带RootController的多视图TableView
我正在尝试使用导航模板创建多视图应用程序。我希望表格位于初始视图的底部,其他视图(图像视图、标签等)位于顶部 最初,我修改了RootViewController.xib以调整tableview的大小,并添加了图像视图,但除了全尺寸的表外,没有显示任何内容 因此,我修改了RootViewController.xib以添加UIView,然后向该视图添加了UITableView和UIImageView。我为tableView添加了IBOutlet。在my RootViewController.xib中,文件的所有者(RootViewController)具有到视图和表视图的出口连接。在UITableView上单击鼠标右键时,我看到文件所有者的引用出口。在UIView上单击鼠标右键时,会看到引用文件所有者的出口。(我没有UIImageView的出口,因为我不在代码中修改;我需要一个吗?) 但是,当我启动应用程序时,它会崩溃,并显示以下消息: “NSInternalInconsistencyException”,Objective c 带RootController的多视图TableView,objective-c,cocoa-touch,uitableview,uinavigationcontroller,Objective C,Cocoa Touch,Uitableview,Uinavigationcontroller,我正在尝试使用导航模板创建多视图应用程序。我希望表格位于初始视图的底部,其他视图(图像视图、标签等)位于顶部 最初,我修改了RootViewController.xib以调整tableview的大小,并添加了图像视图,但除了全尺寸的表外,没有显示任何内容 因此,我修改了RootViewController.xib以添加UIView,然后向该视图添加了UITableView和UIImageView。我为tableView添加了IBOutlet。在my RootViewController.xib中
原因:'-[UITableViewController loadView]加载了“RootViewController”nib,但未获取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;
}
您之所以会出现此错误,是因为当您创建基于导航的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;
}
好的,我在RootViewController.h中将UITableViewController更改为UIViewController;现在图像视图显示,空白表也显示。似乎是一个常规的基于视图的应用程序,而不是基于导航的应用程序。在这种情况下,是否仍然有理由使用导航模板?好的,到目前为止仍然有效;但是,我在显示单元格属性时遇到问题。从[array objectAtIndex:row]消息中输入load cell.textLabel.text,并正确显示;但是,当我尝试设置附件类型或图像时,它不会显示:cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;cell.imageView.image=[UIImage ImageName:@“myImage.png];因此我无法显示这些属性。非常感谢您的任何想法。您能发布整个
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)吗indexPath
方法?什么意思?在Xcode中选择代码,复制它,然后单击问题下的“编辑”并将其粘贴。粘贴时确保使用代码格式。我剪切并粘贴到这个“添加注释”框中,但它只显示nnn个字符,按“添加注释”按钮没有任何作用。。。