我似乎无法从逻辑上弄清楚如何为每个分组的节分别创建一个数组,该数组将在导航控制器的详细视图中显示不同的数据。对于每个组,都有不同的数据,但是第二个视图当前在其中显示一个数组中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;
}
OK,现在回到实现文件(仍然是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之间创建一个新的序列。选择所需的任何类型,但您可能应该使用
Replace
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
方法中观看它的方式。它告诉委托人现在选择了指定的行。使用此功能,您可以有选择地对单元格点击执行操作。