- (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]];
}
}我有两个表视图要连接,但我不知道这是不是正确的代码。它运行,然后当我单击一个单元格时,它崩溃了。有人能帮我吗?
发布于 2014-02-07 01:42:46
当您选择单元格时,会创建一个新的VC,但它永远不会出现。使用
[self.navigationController pushViewController:Species animated:YES];来展示它。
也可以按住Ctrl键并从第一个VC中的单元格拖动到第二个VC (在Storyboard中),然后使用以下代码:
- (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!
发布于 2014-02-07 01:11:47
你用的是故事板还是笔尖?
如果你使用故事板,你需要实现prepareForSegue,而如果你使用nibs,那么你需要在didSelectRowAtIndexPath中将新的视图控制器推送到堆栈上。
https://stackoverflow.com/questions/21608978
复制相似问题