2014-02-06 45 views
0
- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *CI= @"GenusNameCell"; 

    GenusNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CI]; 
    if (cell == nil) { 
     cell = [[GenusNameCell alloc] initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:@"GenusNameCell"]; 

     [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
    } 
    int row = [indexPath row]; 

    cell.GenusNameLabel.text = [genusName objectAtIndex:indexPath.row]; 
    cell.GenusCommonNameLabel.text = _genusCommonName[row]; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    SpeciesViewController *Species = [[SpeciesViewController alloc] initWithNibName:@"SpeciesViewController" bundle:nil]; 
    if ([[genusName objectAtIndex:indexPath.row] isEqual:@"Abelia"]) { 
     Species.SpeciesInt = 0; 
     [Species setTitle:[genusName objectAtIndex:indexPath.row]]; 
    } 
} 

我有2個tableviews連接,但我不知道這是否是正確的代碼來做到這一點。它運行的時候,當我點擊它崩潰的單元格。有人能幫我嗎?如何從桌面推到桌面

+3

那你爲什麼要包含這段代碼,而不是'didSelectRowAtIndexPath:...'中的代碼? –

+1

你有沒有實現的 - (空)的tableView:(UITableView的*)的tableView didSelectRowAtIndexPath方法:(NSIndexPath *)indexPath – Jared

+0

- (空)的tableView:(UITableView的*)的tableView didSelectRowAtIndexPath方法:(NSIndexPath *)indexPath { SpeciesViewController *核素= [ [SpeciesViewController alloc] initWithNibName:@「SpeciesViewController」bundle:nil]; if([[genusName objectAtIndex:indexPath.row] isEqual:@「Abelia」]) { Species.SpeciesInt = 0; [Species setTitle:[genusName objectAtIndex:indexPath.row]]; } – user3228210

回答

0

您是否正在使用故事板或筆尖?

如果您正在使用storyboard,則需要實現prepareForSegue,而如果使用的是nib,則需要將新的視圖控制器推送到didSelectRowAtIndexPath的堆棧中。

+0

@ user3228210我無法對上述內容添加評論,因爲我沒有50的聲望,所以希望您能看到此內容。沒有看到你的故事板和模型是如何設置的,很難告訴你它到底出了什麼問題。你能否提供一個鏈接到你的項目的壓縮版本,我會看看? – Nick

1

當您選擇單元格時,會創建一個新的VC,但它永遠不會呈現。使用

[self.navigationController pushViewController:Species animated:YES]; 

來呈現它。

您也可以按住Ctrl鍵從細胞中第一個VC的第二VC(在故事板),並使用此代碼:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([segue.identifier isEqualToString:@"nameOfYourSegue"]) { 
     if ([[segue destinationViewController] isKindOfClass:[SpeciesViewController class]]) { 
      SpeciesViewController *Species = (SpeciesViewController *)[segue destinationViewController]; 
     Species.SpeciesInt = 0; 
     [Species setTitle:[genusName objectAtIndex:indexPath.row]]; 
     } 
} 

記住設置segueIdentifier在故事板!

+0

我有代碼的頂部,但prepareForSegue方法,我把它放在cellForRowAtIndexPath或didSelectRowAtIndex? – user3228210

+0

prepareForSegue在執行segue之前會自動調用,因此您只需將它像其他任何方法一樣放入實現文件中即可。應用程序崩潰時會得到什麼錯誤消息? – Jorn

+0

我把prepareForSegue方法。它工作沒有崩潰,但是當我點擊一個單元格它會去下一個tableview和加載1陣列,但它加載相同的數組,我按下,即使我有3個不同的陣列 – user3228210