在远程数据库上开发完整的 CRUD 应用程序。发布此问题时,我的应用程序运行正常。但是我想推进它。我是IOS开发的新手。正如您从我的代码中看到的那样,我正在获取 valueForKey 名称并将其显示在 tableview 中。单击名称会带您进入屏幕,其中包含文本字段中的名称。但是,我想获取以下键的 valueForKey:namesid、address1、address2、address3、address4、email、mobile、telephone 和 comment,并将它们显示为名称,所有值都将显示在它们自己的文本字段中。
- (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;
}