我現在的問題是我在這段代碼上有數組。用JSON字典填充UITableView請求iOS
需要用URL請求替換UItableView JSON響應對象並替換字典值。
有一種方法可以清理信息並重新裝載表以刷新新數據或可以方便實現的數據?
#import "RecipeTableViewController.h"
#import "RecipeTableCell.h"
#import "RecipeDetailViewController.h"
#import "Recipe.h"
@interface RecipeTableViewController()
@end
@implementation RecipeTableViewController
{
NSArray *recipes;
NSArray *searchResults;
NSMutableArray *myObject;
// A Dictionary Object
NSDictionary *dictionary;
NSString *name;
NSString *subject;
NSString *avatar;
NSString *rate;
NSString *bio;
}
- (void)viewDidLoad
{
[super viewDidLoad];
name = @"name";
subject = @"subject";
avatar = @"avatar";
rate = @"rate";
bio = @"bio";
myObject = [[NSMutableArray alloc] init];
NSData *jsonSource = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://latamig.com/data.json"]];
id jsonObjects = [NSJSONSerialization JSONObjectWithData: jsonSource options:NSJSONReadingMutableContainers error:nil];
for (NSDictionary *dataDict in jsonObjects)
{
NSString *name_data = [dataDict objectForKey:@"name"];
NSString *subject_data = [dataDict objectForKey:@"subjects"];
NSString *avatar_data = [dataDict objectForKey:@"avatar_s"];
NSString *rate_data = [dataDict objectForKey:@"user_rate"];
NSString *bio_data = [dataDict objectForKey:@"bio"];
NSLog(@"Name: %@", name_data);
NSLog(@"Subject: %@",subject_data);
NSLog(@"Avatar: %@", avatar_data);
NSLog(@"Rate: %@",rate_data);
NSLog(@"Bio: %@",bio_data);
dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
name_data,name,
subject_data,subject,
avatar_data,avatar,
rate_data,rate,
bio_data, bio, nil];
[myObject addObject:dictionary];
}
// API CALL MAKE END
/* Initialize the recipes array
Recipe *recipe1 = [Recipe new];
recipe1.name = @"Andruw";
recipe1.prepTime = @"$255/h";
recipe1.bio = @"I'm a Web developer working for 3 years as a Programmer on Facebook";
recipe1.image = @"avatar1.jpg";
recipes = [NSArray arrayWithObjects:recipe1, nil];
*/
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
} else {
return [recipes count];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 71;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomTableCell";
RecipeTableCell *cell = (RecipeTableCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[RecipeTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Display recipe in the table cell
Recipe *recipe = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
recipe = [searchResults objectAtIndex:indexPath.row];
} else {
recipe = [recipes objectAtIndex:indexPath.row];
}
cell.nameLabel.text = recipe.name;
cell.bioLabel.text = recipe.bio;
cell.thumbnailImageView.image = [UIImage imageNamed:recipe.image];
cell.thumbnailImageView.clipsToBounds = YES;
cell.thumbnailImageView.layer.cornerRadius = cell.thumbnailImageView.frame.size.width /2;
cell.prepTimeLabel.layer.cornerRadius = 5;
cell.prepTimeLabel.text = recipe.prepTime;
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showRecipeDetail"]) {
NSIndexPath *indexPath = nil;
Recipe *recipe = nil;
if (self.searchDisplayController.active) {
indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
recipe = [searchResults objectAtIndex:indexPath.row];
} else {
indexPath = [self.tableView indexPathForSelectedRow];
recipe = [recipes objectAtIndex:indexPath.row];
}
RecipeDetailViewController *destViewController = segue.destinationViewController;
destViewController.recipe = recipe;
}
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
searchResults = [recipes filteredArrayUsingPredicate:resultPredicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
@end
所以據我所知,Xcode沒有專門的設備來處理JSON。 –
(你也許是指iOS或OSx?) –
你有問題嗎? –