jwknz
jwknz

Reputation: 6824

UITableViewController vs UITableView

I have 2 Controllers within one app as I am trying out these different settings.

One is a UITableViewController which the way I understand has the Delegate and the Datasource are drawn from the Superclass.

Now going by that theory I now also have a UIViewController with a UITableView on it and in the header file I have declared the Datasource and the Delegate.

So far so Good:-)

Now this is the bit I do not get:

In the implementation file I can use the tableView pointer to link data, but I am using Core Data and I have this piece of code amongst others:

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
  {   
  [self.tableView beginUpdates];
  }

This and 5 other situations give an error on the UIViewController if I do not setup a

IBOutlet UITableView *tableView;

Of course it is synthesized:-)

But if I declare this tableView I get a warning for a double up on the line that says this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}

because it also declares a tableView.

So I have 2 questions:

1) Why do I have to set the IBOutlet if I have set a UITableViewDatasource and Delegate?

2) If I give the UITableViewCell a different pointer name - like tableView2, why can I not see the changes upon editing the data, but only see the result after relaunching the app?

The MasterViewController, which is the UITableViewController has not got this option.

Cheers Jeff

---ADDING IMAGES----

enter image description here

enter image description here


To get into edit mode i have this code in the viewDidLoad

self.navigationItem.leftBarButtonItem = self.editButtonItem;

and I use this code to action what needs to be done:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{ if (editingStyle == UITableViewCellEditingStyleDelete) { NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext]; [context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];

    NSError *error = nil;
    if (![context save:&error]) {
        // Replace this implementation with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
}   
}

Now the + option works, but I only see the result after I relaunch the app. I can swipe and delete, but again it does not animate live within the same instance of the app.

Upvotes: 1

Views: 1130

Answers (1)

jrturton
jrturton

Reputation: 119272

You have declared a property called tableView and presumably just synthesized it with the same name.

UITableViewController also has a property called tableView, but the backing ivar will be called something different, probably _tableView, precisely because the default tableview datasource and delegate methods usually pass in an argument called tableView which, in the scope of the method, is clashing with your instance variable.

Inside that method, what does tableView mean? The argument that was passed in, or your ivar?

In answer to your specific questions:

  1. You need the outlet for those instances when your view controller needs to talk to the table view (like when you tell it to begin updates). The datasource and delegate connections are there for when the table view needs to talk to your view controller (like when it asks for a cell).

  2. I'm not sure what you are asking here, you can call the outlet to the table view anything you like but you must also alter everything that sends messages to self.tableView. For consitency I would synthesize the property like so:

    @synthesize tableView = _tableview;
    

    This prevents an ivar called tableView existing in the first place. I am assuming you haven't declared the ivar, and have only declared the property as an IBOutlet?:

    @property (nonatomic) IBOutlet UITableView *tableView;
    

Upvotes: 1

Related Questions