How to map UITableView cell to a remote database backend? - iphone

I'm making a simple To-Do list app for the iPhone with a rails backend.
So far, I can pull all the tasks in a To-Do list from the web app, parse the JSON, and insert it into the UITableView.
The json response from rails:
[
{"id":1,"name":"Buy bread"},
{"id":8,"name":"Get gas"},
{"id":14,"name":"Call John"}
]
In the UITableView, a row for each task, the textLabel of the cell is the name of the task:
The problem I'm having now is how to associate the cell with the record id of the task.
Two tasks can have the same name, so I can't just get the id from the hash based only on the name.
I need the record id, so I can post back to the rails app, after the user edits the UItableView (deletes a task for example).
Is there a property I can set on the cell? I feel like I'm missing something obvious. This is my first iPhone app, so please be gentle, hehe.

How are you getting the JSON array to an Objective-C array for use in the table? You'll have to do that again for the ID of the task, and in your - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method use something like theID = [idArray objectAtIndex:indexPath.row];

The better solution would be to maintain a proper data model on the client. You could use the JSON as your model, but Core Data is a bit more robust. What you'd want is to have your controller associate what it knows about the view with what it knows about the model. For instance, if you use didSelectRowAtIndexPath: You'd want to use this index path to map to your model.
Your solution of setting a property on a cell which allows a client of your view to query your data model is in violation of the rules of the MVC paradigm. It couples your view to your model which isn't great.

Related

Parse.com: PFQueryTableViewController with PFUser

I am using the Parse.com framework with my iOS app. I want to have two section in an UITableView. I read that I can use the PFQueryTableViewController to query data from the parse cloud and have it in a UITableView. How can I use PFQueryTableViewController to query the User class usernames to be in one section of the UITableView, and the friends of the currentUser to be in the other section of the UITableView, friends is an array variable that I made every PFUser have. How would I do this?
The PFQueryTableViewController class is really intended to be used for a single query only - your requirement needs two queries to be performed (one for the currentUsers's friends, and one for all User's usernames. If I were you I'd implement my own table view controller that performs the two queries in the background separately, and manages its own calls to reloadData when the data is returned from Parse for each section of the tableView.

Call specific web service on -didSelectRowAtIndexPath

I am implementing the application which consumes webservice (first authentication and then calling different webservices with provided token as a header field in URL)
After the authentication phase, I am showing the Home page to the user.
On Home page i am showing UITableView (with 9 cells on it.)
If those were the buttons I would write separate 9 - "onButtonClick" functions to call 9 different webservices, accordingly.
What can be best implementation (or flow), for implementing this structure.
Let me know if you want more details.
Thank you.
You could keep the URLs for the services in an array attached to the table view's view controller (or delegate really) and then use the selected row as an index into the array and call the service.
No need to write nine methods separately
you can use the JASON COCO way as he said store the URL in an array.
when you will click on a particular row. you should pass that URL to Web services method
suppose you have
- (void)tableView:(UITableView *)tableView1 didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString* urlString=[self.URLArray objectAtIndex:indexPath.row];
[self callWebSerciceWithUrl:urlString];
}
-(void)callWebSerciceWithUrl:(NSString* )Url{
//Use that URL String for calling web services...
//You Should write your Remaining web service Code here...
}

Using KVO to reload data in UITableView

I've been expanding my horizons recently and am trying to start utilizing KVO more in my programming.
I have a view controller in my iPhone application which acts as the datasource and delegate for a UITableView. I also have a singleton model controller which coordinates populating my model with data fetched from the web.
In my view controller, I request that the model's controller load new data from the web. Then I can observe the "dataset" property of the singleton and receive KVO notifications when items are added to or removed from the set.
Now, each cell in my table view has an indicator which specifies whether the content in that cell has been read or not (like the blue "unread" dot in mail). Like mail, when a row is selected, I'll display details about that row. In the viewDidLoad for the detail view, I set the object's "read" property to YES. I would like the original view controller to be able to observe this "read" property of each object in the dataset, so that [tableView reloadData] can automatically be called as necessary and redraw the cells without the blue dot.
In researching this, I found the following link: http://homepage.mac.com/mmalc/CocoaExamples/controllers.html#observingACollection
According to this, it looks like I will do the following:
1) Be an observer of the array
2) Whenever I get a notification of a change to the array, I add (or remove) myself as an observer for the individual properties I am interested in.
3) When I get a notification of a change to the property I'm interested in, I can call [tableView reloadData]
I'm currently in the process of attempting to implement this approach. Can anyone with experience doing this offer some advice on this approach? If this the best way to handle this type of situation?
If this is the correct approach, would anyone be willing to share their implementation of adding/removing the observers for objects in the collection when the collection changes?
Thanks!
I think you can accomplish this task by using Core Data and the Fetched Results Controller.
I'm sure this can save you a lot of work.
Here's a good guide: Ray Wenderlich Core Data Tutorial, getting started

Json - iPhone app: uitableview and uiwebview

I am quite new to Xcode and Json.
In my app, I have navigation through pages via tableviews (drill down). What I want to do now, is to use Json to create my tableviews and then when I select a cell in the uitableview, it will go to a website through the next page via a uiwebview.
How will I do this?
Parse your json using a parser
Create the parsed response as a data model for table view. You can use a Dictionary/array to keep things simple
Implement the tableview delegate methods and get the selected index
On didselectatrowindex method you create a new view, fetch the url from your model and load it into the webview.
I could write some code but thats really too much you are asking for. If you get stuck in any of the steps above, do leave a comment. I will be glad to help you out.

Save current data from UITableView

Trying to figure out how data storage from UITableViews is ment to work. What is good practices when saving a UITableView data on e.g. viewDidDisappear.
Do I have to update an array with every changes made while working in the view, or can I collect all current values form the table on exit view?
Can someone point me in the right direction?
Thanks!
First, on when to save the data, I would suggest you save your data in this method instead of viewDidDisappear:.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Second, there are a bunch of ways to store data in iPhone applications, such as plist or sqlite3 database.
You are thinking about the problem the wrong way around. The UITableView only displays the data currently on screen. Its not a place to ever collect data from (other than user interaction which you should collect when they interact - didSelectRowAtIndexPath). Spend some time reading up on Model View Controller. The UITableView data should have COME from a model - the view should simply display it - so there is nothing to collect.