开发完整的C.R.U.D.远程数据库上的应用程序。我的应用程序在发布此问题时正常工作。不过我想提前一点。我是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;
}
答案 0 :(得分:0)
我强烈建议您查看Sensible TableView框架。框架将自动获取远程数据并将其显示在表视图中。与手动操作相比,节省了大量时间。