2011-07-21 12 views
0

所有,表視圖 - Click上聲明如果

我有我需要做出不同的反應被點擊時使細胞與串X在它VS串Y.表視圖X將帶你到X查看( XViewController)和Y會帶你到Y視圖(YViewController)等等等哪裏將是插入此if語句的最佳位置,以及如何讓它過渡到正確的視圖?

感謝

回答

5

希望實現

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

然後你就可以接入小區的問題讀取字符串,並提出相應的視圖控制器。

例如:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath]; 
    if ([cell.text isEqualToString:stringX]) { 
     XViewController *xvc = [[XViewController alloc] init]; 
     [self presentModalViewController:xvc animated:YES]; 
     [xvc release]; 
    } else { 
     YViewController *yvc = [[YViewController alloc] init]; 
     [self presentModalViewController:yvc animated:YES]; 
     [yvc release]; 
} 

然後,取的viewController推,你可以用

[self dismissModalViewControllerAnimated:YES]; 
+0

以及我將如何呈現相應的視圖控制器?界面生成器是否有這個選項,或者我需要手動編寫它嗎? – Baub

+0

@James:我建議你使用'-presentModalViewController:animated:'。 – PengOne

2

關閉它的方法:

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

就是在那裏你會做到這一點。然後你會從指數路徑搶細胞:

UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];

最後,檢查字符串。該代碼應該是這樣的:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;{ 
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath]; 

    if([cell.text isEqualToString:X]){ 
    // load view X and push onto the view controller 
    } 
    else{ 
    // load view Y and push onto the view controller 
    } 
} 

如果使用自定義的細胞,只是看在自定義屬性,而不是的.text。

你真的應該看看這裏:

Apple Navigation Controller Reference

這真的很簡單。只需分配新的視圖控制器,並將其推到導航控制器上:

NewViewController * newView = [[NewViewController alloc] init]; 
[self.navigationController pushViewController:newView animated:YES]; 
[newView release]; 
+0

好東西,謝謝。我將如何推入下一個視圖控制器? – Baub