0

Developing full C.R.U.D. app on a remote database. My app is working correctly as of publishing this question. However I would like to advance it. I am a newbie to IOS development. As you can see from my code I am getting the valueForKey name and displaying it in the tableview. Clicking on the name brings you to screen with the name in a text field. However I want to get the valueForKey for the following keys: namesid, address1, address2,address3,address4, email, mobile,telephone and comment and display them as for the name all values will be displayed in their own text fields.

- (void)requestFinished:(ASIHTTPRequest *)request
{
//Enable the Reload Button.
self.navigationItem.leftBarButtonItem.enabled = YES;
//Enable the Add Contact Detail Button.
self.navigationItem.rightBarButtonItem.enabled = YES;

searchBar.hidden = NO;

SBJsonParser *parser = [[SBJsonParser alloc] init];

if (request.responseStatusCode == 200) {

    [self.activityIndicator stopAnimating];

    NSString *json_string = [request responseString];

    // Actually parsing the JSON
    NSArray *statuses = [parser objectWithString:json_string error:nil];

    for (NSDictionary *status in statuses){
        @try {

            [remotecontacts addObject:[status valueForKey:@"name"]];

        }
        @catch (NSException *exception) {

            [remotecontacts addObject:[NSString stringWithFormat:@""]];

        }

    } // end for

    NSDictionary *remotecontactsInDict = [NSDictionary dictionaryWithObject:remotecontacts forKey:@"Contacts"];

    [listOfItems addObject:remotecontactsInDict];

    [self.myTableViewController reloadData];


} else {

    //Enable the Reload Button.
    self.navigationItem.leftBarButtonItem.enabled = YES;
    //Enable the Add Contact Details Button.
    self.navigationItem.rightBarButtonItem.enabled = YES;

    //Stop the Activity Indicator.
    [self.activityIndicator stopAnimating];

    UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle:@"Error"   message:@"Unexpected error, please try again later!"delegate:self cancelButtonTitle:@"Ok"otherButtonTitles:nil];
    [myAlert show];
    [myAlert release];
    return;
}


}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:     (NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}

// Set up the cell...

if(searching) 
    cell.text = [copyListOfItems objectAtIndex:indexPath.row];
else {

    //First get the dictionary object
    NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];

    NSArray *array = [dictionary objectForKey:@"Contacts"];
    NSString *cellValue = [array objectAtIndex:indexPath.row];
    cell.text = cellValue;

}

return cell;

}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

if (searching)
    return [copyListOfItems count];
else {

    //Number of rows it should expect should be based on the section
    NSDictionary *dictionary = [listOfItems objectAtIndex:section];
    NSArray *array = [dictionary objectForKey:@"Contacts"];
    return [array count];

}

}

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

//Get the selected country

NSString *selectedCountry = nil;

if(searching)
    selectedCountry = [copyListOfItems objectAtIndex:indexPath.row];
else {

    NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
    NSArray *array = [dictionary objectForKey:@"Contacts"];
    selectedCountry = [array objectAtIndex:indexPath.row];
}

//Initialize the detail view controller and display it.
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:[NSBundle mainBundle]];
dvController.selectedCountry = selectedCountry;
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
dvController = nil;

}

David Egan
  • 424
  • 2
  • 8
  • 23

1 Answers1

0

I'd strongly recommend you checkout the Sensible TableView framework. The framework will automatically fetch your remote data and display it in your table view. Saves me tons of time compared to doing this manually.

Matt
  • 2,391
  • 2
  • 17
  • 18
  • Yes Matt thanks for that. I means going back propably to first base and redesign. I have done this app as you say manually. I have the app all most complete, and works very well. It was just to get a little assistance in retreving and displaying additional information the manual way with perhaps some code samples. – David Egan Jul 10 '13 at 11:03