Understanding sample code for adding a row to a table view

Go To StackoverFlow.com

1

I was reading the docs to learn how to add a row in a table view, and I found this example :

- (void)save:sender {

    UITextField *textField = [(EditableTableViewTextField *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]] textField];

    SimpleEditableListAppDelegate *controller = (SimpleEditableListAppDelegate *)[[UIApplication sharedApplication] delegate];
    NSString *newItem = textField.text;

    if (newItem != nil) {
        [controller insertObject:newItem inListAtIndex:[controller countOfList]];
    }

    [self dismissModalViewControllerAnimated:YES];

}

I don't understand the method : insertObject:inListAtIndex: or what [[UIApplication sharedApplication] delegate]; stands for; are we putting the data in a plist file? Could someone explain this to me? The UIApplication docs do not really help.

2012-04-04 18:44
by Paul
Links to the source of your question are helpful, otherwise we just have to guess at the context of your question - mydogisbox 2012-04-04 19:42
@Iulius Cæsar : thanks Iulius Cæsa - Paul 2012-04-04 21:39


2

[[UIApplication sharedApplication] delegate] is the main application delegate, typically this is a class named AppDelegate. The main application delegate is the one that is created on application start-up and which is the main controller for your application.

I'm going to assume that you're using something similar to this class as your AppDelegate class.

[controller insertObject:newItem inListAtIndex:[controller countOfList]];

This assumes that your AppDelegate class has a method named insertObject:inListAtIndex: on it. For the class I linked the method looks like this:

- (void)insertObject:(id)obj inListAtIndex:(NSUInteger)theIndex {
    [list insertObject:obj atIndex:theIndex];
}

So in this case, that method is adding the object to a member variable of your AppDelegate class called List.

2012-04-04 19:42
by mydogisbox
thanks, it makes more sense to me, so the controller should use the appdelegate to store the data? does it make any difference to store the data in the app delegate or in the controller? and if we go on with the same example : at the end of the insertObject method, we add reloadData to refresh the table view? Thank - Paul 2012-04-04 21:28
I think you're a little confused about what the app delegate is. In this case the controller variable IS your app delegate. Part of the problem is that this is not a good example of how to create a uitableviewcontroller. Start http://developer.apple.com/library/ios/#DOCUMENTATION/UserExperience/Conceptual/TableViewiPhone/AboutTableViewsiPhone/AboutTableViewsiPhone.html#//appleref/doc/uid/TP40007451 and if you have more questions post another one - mydogisbox 2012-04-04 21:37
Better link: http://developer.apple.com/library/ios/#DOCUMENTATION/UserExperience/Conceptual/TableViewiPhone/CreateConfigureTableView/CreateConfigureTableView.html#//appleref/doc/uid/TP40007451-CH6-SW1 - mydogisbox 2012-04-04 21:40
no i understood, my question was about the best way to store the data (the simplest way), but i understood it, thanks to your first message, i was looking for an insertObject method in the docs, but it looks like it was a custom method, and that way it makes sense to me. It would be great to find the source code of this project, but i couldn't find it, and i could not open your link. Anyway, i guess reloadData is used in this project, thanks for your hel - Paul 2012-04-04 22:58


0

There is no connection to plist. Just message exchange with help of delegates to change table view.

From apple documentation:

When a table view enters editing mode and when users click an editing control, the table view sends a series of messages to its data source and delegate, but only if they implement these methods. These methods allow the data source and delegate to refine the appearance and behavior of rows in the table view; the messages also enable them to carry out the deletion or insertion operation.

Read this: http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/TableView_iPhone/ManageInsertDeleteRow/ManageInsertDeleteRow.html

And one of the best books - Beginning iphone 4 (or 5)

2012-04-04 19:14
by nik
You could make this answer better by making sure to directly answer all parts of his question - mydogisbox 2012-04-04 20:06
Ads