【发布时间】:2014-05-17 23:30:15
【问题描述】:
我现在的问题是我在这段代码中有数组。
需要将 UItableView 替换为 URL Request JSON 响应 Object 并替换 Dictionary Values。
有一种方法可以清理信息并重新加载表以刷新新数据或可以方便地实现此目的的方法吗?
#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?)
-
这个问题似乎是题外话,因为它不是一个问题。
-
也许如果您改写并清楚地将您的问题表述为“我如何...”或“为什么这会引发错误...”等,我们可以更好地回答您的问题。此外,如果您可以将代码示例隔离到较小的部分,而不是发布整个视图控制器。这会让事情变得更容易。
-
Eddwin,您的问题有点不清楚。最好的办法是查看这方面的众多教程之一...nscookbook.com/2013/03/… ..提示请务必只查看最近的教程,因为今天,在 iOS 中使用 JSON 非常容易。 (在过去你必须使用图书馆等)
标签: ios json uitableview