0

我似乎无法从逻辑上弄清楚如何为每个分组部分设置一个单独的数组,每个部分在导航控制器的详细视图中显示不同的数据。对于每个组,都有不同的数据,但是第二个视图当前在 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";
    }
}
4

2 回答 2

1

The way that you would pass data along would be by using a segue, which is what you are doing. The thing that you are not doing correctly is in the prepareForSegue method. Just to simplify things, use the didSelectRowAtIndexPath method, so you aren't creating your own way of finding the selected cell.

To handle that, you will have to add a little to the didSelectRowAtIndexPath. Here is what you need to do:

The:

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

method that you have, should look like this:

-(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

    }
}

Now you want to create the array selectedAddressBook. So in the header (we're still in the MasterViewController):

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

OK, now back to the implementation file (still MasterViewController). Now you are going to use the didSelectRowAtIndexPath method (I'll give an explanation on the importance of this at the end).

- (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];
}

If you try and run it now, it will probably crash. Remove the existing segue. Just delete it. Create a new segue between the MasterViewController itself and the DetailViewController. Select whatever type you want, but you should probably use the Replace segue. Give that segue the same identifier as the one you just deleted.

Now, in the DetailViewController .m file put an NSLog to watch for data in the 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
}

If all went well, then the Log should show the details ONLY for the row that was selected. Now in the DetailViewController, you can access the data from the array and display it however you want to.

The reason you need to yu the didSelectRowAtIndexPath method when using a tableView, is really just because is makes life simple. This method recognizes the row that was selected, by using NSIndexPath. Which does away with the way you were watching it in your prepareForSegue method. It tells the delegate that the specified row is now selected. Using that, you can selectively perform actions on cell taps.

于 2013-09-08T20:40:46.350 回答
0

我对你试图用addressBook上面的数组做什么感到有点困惑。其余的,看起来你应该使用字典。你熟悉字典吗?您可以将键设置为县,并将值设置为该县的数组。然后当你问numberOfSectionsInTableViewreturn [myDictionary allKeys].count

让我知道这是否有意义。如果没有,我会进一步详细说明。另外,描述其目的addressBook是什么。

于 2013-09-08T17:17:53.267 回答