gpt4 book ai didi

iphone - UITableView 分组数组

转载 作者:行者123 更新时间:2023-11-29 03:39:15 25 4
gpt4 key购买 nike

我似乎无法从逻辑上弄清楚如何为每个分组部分建立一个单独的数组,每个分组部分将在导航 Controller 的详细 View 中显示不同的数据。对于每个组,都有不同的数据,但第二个 View 当前显示一个数组中 UILabel 中的数据,并从每个组的数组的开头开始。如果您需要更多信息,请告诉我。

- (void)viewDidLoad
{
[super viewDidLoad];

//Keeping of Business Names for Detailed View
addressBook=@[@"Clinic 1 Business",@"Clinic 2 Business",@"Clinic 3 Business"];

//holding of clinic names per county
adams=@[@"Clinic 1 Business Listing",@"Clinic 2 Business Listing",@"eeeee"];
allegheny=@[@"Clinic 3 Business Listing",@"Clinic 4 Business Listing"];
armstrong=@[@"Clinic 5 Business Listing",@"Clinic 6 Business Listing",@"Clinic 7 Business Listing"];

// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;


NSLog(@"this is the content of addressBook: \n %@", addressBook);

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

// Return the number of sections.
return 3;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

// Return the number of rows in the section.
//return addressBook.count;
if(section==0)
{
return [adams count];
}
else if(section==1)
{
return [allegheny count];
}
else if (section==2)
{
return [armstrong count];
}
}

/*
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return [NSArray arrayWithObjects:@"Rec", @"A", @"B", nil ];
}
*/

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"abCell";
addressBookCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

if(indexPath.section==0)
{
cell.addressBookLbl.text=[adams objectAtIndex:indexPath.row];

}
else if(indexPath.section==1)
{
cell.addressBookLbl.text=[allegheny objectAtIndex:indexPath.row];
}
else if (indexPath.section==2)
{
cell.addressBookLbl.text=[armstrong objectAtIndex:indexPath.row];
}

return cell;
}


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier]isEqualToString:@"addressBookDetails"]) {

addressBookDetailsViewController *addressDetailsController = [segue destinationViewController];

NSIndexPath *abIndexPath = [self.tableView indexPathForSelectedRow];

int row = [abIndexPath row];

addressDetailsController.addressDetail=@[addressBook[row]];

NSLog(@"this is the content of addressBook: \n %@", addressBook);
}
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section==0) {
return @"Adams County";
}
else if(section==1)
{
return @"Allegheny County";
}
else if(section==2)
{
return @"Armstrong County";
}
}

最佳答案

传递数据的方式是使用segue,这就是您正在做的事情。您没有正确执行的操作是在 prepareForSegue 方法中。为了简化操作,请使用 didSelectRowAtIndexPath 方法,这样您就不会创建自己的查找所选单元格的方法。

要处理这个问题,您必须向 didSelectRowAtIndexPath 添加一些内容。这是您需要执行的操作:

:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

您拥有的方法应该如下所示:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"addressBookDetails"]) {

addressBookDetailsViewController *addressDetailsController = [segue destinationViewController];

addressDetailsController.addressDetail = selectedAddressBook;
//selectedAddressBook is an array that you don't have yet, but will create in the next step
//Make sure that the addressDetail in the DetailViewController is an Array, and not something else

}
}

现在您要创建数组selectedAddressBook。所以在标题中(我们仍在 MasterViewController 中):

@interface  //blah blah blah
{
NSArray *selectedAddressBook;
}

好的,现在回到实现文件(仍然是MasterViewController)。现在您将使用 didSelectRowAtIndexPath 方法(我将在最后解释其重要性)。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//some animation to fade away the blue on selection
[tableView deselectRowAtIndexPath:indexPath animated:YES];

//here you will populate the array that you created
selectedAddressBook = (NSArray *)addressBook[indexPath.row];

//now you perform your segue
[self performSegueWithIdentifier:@"addressBookDetails" sender:self];
}

如果您现在尝试运行它,它可能会崩溃。删除现有的segue。删除它即可。在 MasterViewController 本身和 DetailViewController 之间创建一个新的 segue。选择您想要的任何类型,但您可能应该使用Replace segue。为该 segue 提供与您刚刚删除的标识符相同的标识符。

现在,在 DetailViewController .m 文件中放置一个 NSLog 来监视 viewDidLoad 中的数据:

- (void)viewDidLoad
{
[super viewDidLoad];

NSLog(@"Did the array get passed along? Let's see :\n %@", addressDetail);
//addressDetail should be the name of the array in this VC
}

如果一切顺利,那么日志应该仅显示所选行的详细信息。现在,在 DetailViewController 中,您可以访问数组中的数据并按照您想要的方式显示它。

在使用 tableView 时需要使用 didSelectRowAtIndexPath 方法的原因实际上只是因为它让生活变得简单。此方法使用 NSIndexPath 识别所选的行。这消除了您在 prepareForSegue 方法中观看它的方式。 它告诉代理指定的行现在已被选中。使用它,您可以有选择地对单元格点击执行操作。

关于iphone - UITableView 分组数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18686316/

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