gpt4 book ai didi

ios - 如何根据在 iOS7 上使用 Storyboard单击的行,将不同的数据从不同的数组加载到 TableView 中?

转载 作者:行者123 更新时间:2023-11-29 02:52:17 25 4
gpt4 key购买 nike

我正在学习 iOS 开发并致力于示例 iOS 应用程序。我对 UITableView 有一些疑问,我希望根据单击的行从不同的数组中将不同的数据加载到 TableView 中。我使用带有 nib 文件的 xcode 4 完成了此操作,我在实现文件中编写了 didSelectRowAtIndexPath 方法

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
PetsTableViewController *pets = [[PetsTableViewController alloc]initWithNibName:@"PetsTableViewController" bundle:nil];
if([[petsArray objectAtIndex:indexPath.row ]isEqual:@"Dog"]) {
pets.petsInt = 0;
[pets setTitle:[petsArray objectAtIndex:indexPath.row]];
}
if([[petsArray objectAtIndex:indexPath.row ]isEqual:@"Cat"]) {
pets.petsInt = 1;
[pets setTitle:[petsArray objectAtIndex:indexPath.row]];
}
if([[petsArray objectAtIndex:indexPath.row ]isEqual:@"Snake"]) {
pets.petsInt = 2;
[pets setTitle:[petsArray objectAtIndex:indexPath.row]];
}
[self.navigationController pushViewController:pets animated:YES];
// [pets release];
}

这是示例代码,这里我在表 1 中声明了狗、猫、蛇数组。当我单击该行中的任何一行时,它应该显示

dog -> dog1、dog2、dog3 数组,每行在不同的表中(表 2)。

如何使用带有 xcode 5 的 Storyboard来做到这一点? (使用 segues )

请谈谈你的看法

最佳答案

首先,让我简化您的代码

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
PetsTableViewController *pets = [[PetsTableViewController alloc]initWithNibName:@"PetsTableViewController" bundle:nil];
if([petsArray[indexPath.row] isEqualToString:@"Dog"]) {
pets.petsInt = 0;
[pets setTitle:petsArray[indexPath.row]];
}
if([petsArray[indexPath.row] isEqualToString:@"Cat"]) {
pets.petsInt = 1;
[pets setTitle:petsArray[indexPath.row]];
}
if([petsArray[indexPath.row] isEqualToString:@"Snake"]) {
pets.petsInt = 2;
[pets setTitle:petsArray[indexPath.row]];
}
[self.navigationController pushViewController:pets animated:YES];
}

其次,如果你想初始化你在 Storyboard上创建的 Controller ,你应该使用这行代码:

PetsTableViewController *pets = [[UIStoryboard storyboardWithName:@"Main"bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"PetsTableViewController"];

记得设置你的 StoryboardID

第三,如果你想让pushed view controller在table上显示dog1, dog2, dog3,你可以在PetTableViewController.h中创建一个NSArray属性,然后使用它和往常一样。

第四,您的代码应该可以正常工作,在这种情况下您不需要使用 segue。但是,如果您想使用 segue,只需打开您的 Storyboard并单击您的原型(prototype) UITableViewCell 并按住 Ctrl 键将其拖动到 PetTableViewController,将 segue 设置为 Push。

在您的 Controller 中,实现 prepareForSegue 方法,为 destinationViewController 设置您的 petArray 就这样。

//------------------------------------更新:

  1. 你在 Storyboard中设置了你的 PetViewController 错误,删除它并添加另一个 UITableViewController

  2. 您忘记在您的MainStoryboard 中设置Storyboard ID,所以这行将不起作用PetsTableTableViewController *pets = [[UIStoryboard storyboardWithName:@"Main"bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"PetsTableViewController"];

尝试在 Storyboard中找到它。

  1. 不要将UITableViewCell 拖到PetViewController(我错了,因为我只用过一次)。相反,将 ViewController 拖到 PetViewController。如果您正确执行此操作,那么这些代码行应该可以正常工作:

    PetsTableTableViewController *pets = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"PetsTableTableViewController"];
    [self.navigationController pushViewController:pets animated:YES];

或者使用segue

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"showListDetails"])
{
PetsTableTableViewController *destViewController = segue.destinationViewController;
[destViewController setPetsInt:selectedIndex.row]; //selectedIndex is a class variable of NSIndexPath
}
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedIndex = indexPath;
[self performSegueWithIdentifier:@"showListDetails" sender:self];
}

关于ios - 如何根据在 iOS7 上使用 Storyboard单击的行,将不同的数据从不同的数组加载到 TableView 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24358383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com