2014-02-06 67 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