我想鏈接單元格從UITableViewController到UIViewController,但每個單元格都必須調用不同的UIViewController(所以我想象我必須創建一個鏈接到每個視圖控制器),我不知道如何通過故事板鏈接它們。如何將一個UITableViewController鏈接到多個ViewControllers以及如何調用它們
這是我到現在爲止:
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等)。
謝謝!
這裏使用不同的行並從該行調用不同的控制器...意味着如果你想訪問你的第二部分和第一行然後右:if(indexPath.section == 1) { if(indexPath.row = = 0) { } } – Vishal
知道了並且努力了!所以我不必在故事板中鏈接它們,對吧?好簡單!我剛剛收到了一個警告'presentModalViewController:animated is deprecated:first deprecated in iOS 6.0',你知道我可以用什麼來代替嗎? – Adami
替換爲以下行:[self presentViewController:pNewController animated:YES completion:nil]; – Vishal