【发布时间】:2013-01-21 03:02:39
【问题描述】:
我正在尝试将单元格从 UITableViewController 链接到 UIViewController,但每个单元格都必须调用不同的 UIViewController(所以我想我必须为每个视图控制器创建一个链接)而且我不知道如何通过情节提要链接它们。
这是我到现在为止的:
MainView.h
@interface MainView : UITableViewController <UITableViewDelegate, UITableViewDataSource>
{
NSArray *tableData;
}
@property (nonatomic, retain) NSArray *tableData;
@end
MainView.m
- (void)viewDidLoad
{
tableData = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", @"4", nil];
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UIViewController *anotherVC = nil;
if (indexPath.section == 0) //here I've also tried to use switch, case 0, etc
{
if (indexPath.row == 0)
{
anotherVC = [[ViewForFirstRow alloc] init];
}
NSLog(@"anotherVC %@", anotherVC); // shows that anotherVC = ViewForFirstRow
[anotherVC setModalPresentationStyle:UIModalPresentationFormSheet];
[anotherVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self.navigationController pushViewController:anotherVC animated:YES];
}
}
@end
所以,当第一行被选中并且我没有将 UITableViewController 链接到 UIViewController (ViewForFirstRow) 时,它会显示黑屏。
如果我将 UITableViewController 链接到 ViewForFirstRow,它会打开视图但没有我在代码 [self.navigationController pushViewController:anotherVC animated:YES]; 中使用的效果,所以它似乎是由于链接而不是因为代码 [self.navigationController pushViewController:anotherVC animated:YES]; 而调用视图
如何通过代码正确调用视图,我应该在情节提要中使用哪种链接?另外,如何将 UITableViewController 链接到多个 UIViewController(ViewForFirstRow、ViewForSecondRow、ViewForThirdRow 等)。
谢谢!
【问题讨论】:
标签: iphone objective-c xcode4.5