Search code examples
iosdelegatesuitableviewtableviewuisplitviewcontroller

Two UITableView Controllers in SplitViewController in iPad, Delegate


enter image description here

Hi This is my first iPad app and trying to port my iphone app to iPad. I have followed all the tutorials from http://www.raywenderlich.com/ still having a problem.

Also review this question and still having the problem . Splitviewcontroller with two tableviews, delegate problem

Basically, I have two UITableViewControllers in SplitViewController and when I click the tableview cell in root view controller, I want to populate the details in DetailsViewController in Right side on another Tableview.

The problem is I can manage to pass the array data from but I can't call tableview reload method.


Here is the code

LeftViewController

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSUInteger row = [indexPath row];

    if (row == 0){

        NSLog(@"Row 0 Pressed");  

         RightViewController *rightvc = [self.storyboard instantiateViewControllerWithIdentifier:@"displayenglish"];

        _locallayleft = [ConversationDatabase database].conversationsInfos;

        NSLog(@"Just pushed the array"); 

        rightvc.detailItem = _locallayleft;
        rightvc.title = @"Greetings";


    }

    else if (row == 1) {
        NSLog(@"Row 1 Pressed");  

         RightViewController *rightvc = [self.storyboard instantiateViewControllerWithIdentifier:@"displayenglish"];


        _locallayleft = [ConversationDatabase database].conversationsInfosgeneral;

        rightvc.detailItem = _locallayleft;
        rightvc.title = @"General Conversation";

    }

-----------------------------------------------------------------------------------------
RightViewController


- (void)setDetailItem:(NSArray *)newDetailItem
{

    if(_detailItem != newDetailItem) {

        _detailItem = newDetailItem;

      [self configureView];

    }
}


- (void)configureView
{
    if (self.detailItem) {


        self.locallay = self.detailItem;

        _listOfCoversation = [[NSMutableArray alloc] init];
        for (ConversationInEnglish *c in _locallay)

        {
            NSString *english = c.english;
            NSLog(@"setDetails Item get called");
            NSLog(@"%@",english);

            [_listOfCoversation addObject:english];
        }
        [self.tableView reloadData];
        NSLog(@"Trying to reload TableView");

    }


}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self configureView];

}






#pragma mark - Table view data source

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

    return 1;
}

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

    return [_locallay count];

}

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

    ConversationInEnglish *con = _locallay [indexPath.row];
    _englishLabel = (UILabel *) [cell viewWithTag:200];

    _englishLabel.text = con.english;
    NSLog(@"My data from cell %@",con.english );

    [_englishLabel setFont:[UIFont fontWithName:@"Open Sans" size:22]];

    _myanmarLabel = (UILabel *) [cell viewWithTag:300];
    [_myanmarLabel setFont:[UIFont fontWithName:@"TharLon" size:17]];

    _tonebasedLabel = (UILabel *) [cell viewWithTag:400];
    _tonebasedLabel.text = con.tone_based;


    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"tableviewcell.png"]];

    self.tableView.backgroundColor = background;

    return cell;
}

Solution

  • It looks like when you tap a row in the table on the left, instead of updating the table on the right, you're instantiating a whole new table from the storyboard instead, but not replacing the one on the right with it.

    There isn't enough context here to say exactly how to fix it, but what you'd want to do is when you tap a row in the table on the left, update the table on the right by setting its detailItem property.

    You'll need access to the other table view. There are a few ways to do this depending on how you've got your application set up - if you're using the same left table view on both the iPhone and iPad then you'll probably need some conditional code to locate it, for example:

    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
        DetailViewController *detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
        detailViewController.detailItem = newDetailItem;
    }
    

    Or you could configure it through the storyboard. Either way, the key is to find and update the existing table view instead of instantiating a new one from the storyboard.