C.Johns
C.Johns

Reputation: 10245

tableView:didSelectRowAtIndexPath: call to current-view Parser delegates

I have some pretty interesting logic going, basically I have this data which I want to check over before I display the next view.. just in case the data is empty I want to pop the view, if the data is not empty then I want to load the the view to display it onto the navigational stack.

so in my tableView:didSelectRowAtIndexPath: method, when the selection is made I, get the current selection ID number so I can restrict the values of the data Im going to parse to only related values.

This is what the code looks like inside my method.

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{    
    ///... This stuff is for context.. 

            //Get the subview ready for use
            VSRViewController *vSRViewController = [[VSRViewController alloc] initWithNibName:@"VSRViewController" bundle:nil];
            //Sets the back button for the new view that loads (this overrides the usual parentview name with "Back")
            self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStyleBordered target:nil action:nil];
            //Pass Title over to subview
            vSRViewController.title = @"SubModel";

            //Selected cell gives restult to the subview or o the parent view to be displayed.. when the view is pushed or poped 
            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@",@"MODEL",cell.textLabel.text];
            filterArray = [parsedDataArrayOfDictionaries filteredArrayUsingPredicate:predicate];

            //Sets restriction string so that when subCacheData is parsed only values mathching modIdString will be parsed
            modIdString = [[filterArray valueForKey:@"MODID"] objectAtIndex:0]; //Restricts Mods dataset

            //This sets which if statment to enter in parserDidEndDocument
            dataSetToParse = @"ModID";
            [self startTheParsingProcess:modCacheData];

            //tempModArra is the array I get back from the parser that has had the restriction string applied to it
            if ([tempModArray count] == 0) {
                NSLog(@"POPVIEW"); //testing
                //pop this view to the parent view.. organize all the values that have to be sent back with protocols and delegates
            }else if ([tempModArray count] != 0){

                //Pass the selected object to the new view controller.
                [self.navigationController pushViewController:vSRViewController animated:YES];

                //Check if modIndexPath is same as selected if not remove accessory tick from the subview
                if (![modIndexPath isEqual:indexPath]){
                    submodIndexPath = nil;
                }
                [vSRViewController subModelCachedData:modCacheData indexPath:submodIndexPath dataSetToParse:@"ICSum" modelArray:filterArray modIndexPath:indexPath];

            //.....
            }
            }
    }
    //...

This code has been edited for readability, there is alot of other stuff going on in it.. so some things might be wrong.. as I have edited some of the names.. but its should be fine..

This is whats happening in my parser delegate.

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    //.. other stuff up here.
    if (dataSetToParse == @"ModID") {
        //This applies the 
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@",@"ModID",modIdString]; //modIdString restricts results that have been parsed
        NSArray *filteredArray = [parsedDataArrayOfDictionaries filteredArrayUsingPredicate:predicate];

        tempModArray = filteredArray;

        //what do I do here to get tempModArray back up to tableView:didSelectRowAtIndexPath: method.. this is where I am abit lost.
    }

}

So thats it.. everything works the only issue is that I cannot get my temoModArray back to the tableView:didSelectRowAtIndexPath: so I need some help thinking of a solution.

Also for context the reason I am doing this is that if there are no values in tempModArray I would like to send the user back to the parent view, so they dont see an empty tableview when going to the subview to make a selection.. hope it all make sense.. I look forward to our reply.

Upvotes: 1

Views: 599

Answers (1)

Rayfleck
Rayfleck

Reputation: 12106

what do I do here to get tempModArray back up to tableView:didSelectRowAtIndexPath: method

Short answer: you don't.

didSelectRow has already done his job which is to inform the app that the user selected the row. Now the app has some work to do. Namely, figure out if it's going to push a new view controller with data, or not. So don't push, decide if there's data, and then pop; rather, don't push in the first place if there's no data.

At the point where your parser knows whether it has data or not, you have lots of options. I'm assuming your parser delegate is not in your table view controller class. You can:

  • Post an NSNotification that your table view controller is listening for, and the listening method can either push the detail view controller if there's data, or no-op if there's not. You can pass the array in the notification.
  • Call a method directly on your table view controller to push the detail view controller, passing in the array (declare a protocol in your table view controller header and have the parser delegate call into that method)
  • Push the detail view controller directly from the parser (kind of icky)
  • Use KVO

imo the protocol method is the cleanest, (loose coupling but with good naming), but each to their own.

Upvotes: 1

Related Questions