在 iOS 中获取 JSON 数据并填充 TableView

发布于 2024-12-09 07:08:22 字数 5444 浏览 1 评论 0原文

晚上好!

我需要一些帮助。我从 Google Places API 获取了一些 JSON 格式的数据,但我无法在 iPad(基于 SplitView 的应用程序)中填充 TableView。我是从iOS开发开始的,所以可能有很多错误!我使用了一个项目示例,该示例使用 Twitter API 来检索帖子,只是重命名了 JSON 数据名称。

我在项目中使用了四个文件并实现了该功能:

SimpleSplitController.h
SimpleSplitController.m
SplitSampleAppDelegate.h
SplitSampleAppDelegate.m

我在 SplitSampleDelegate.m 文件中收到错误,因为它在那里进行了检查...

如果有人可以帮助我,我将非常感激!

下面是实现的代码:

SplitSampleAppDelegate.h

#import <UIKit/UIKit.h>

@class APTabBarControllerForSplitController;

@interface SplitSampleAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {

    NSMutableData *responseData;
    NSMutableArray *tweets;

}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) IBOutlet APTabBarControllerForSplitController *tabBarController;

@property (nonatomic, retain) NSMutableArray *tweets;

@end

SplitSampleAppDelegate.m

#import "SplitSampleAppDelegate.h"
#import "APTabBarControllerForSplitController.h"

@implementation SplitSampleAppDelegate

@synthesize window=_window;

@synthesize tabBarController=_tabBarController;

@synthesize tweets;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.tabBarController.delegate = self;

    // Override point for customization after application launch.
    // Add the tab bar controller's current view as a subview of the window
    // Add the view controller's view to the window and display.
    responseData = [[NSMutableData data] retain];
    tweets = [NSMutableArray array];
    NSURLRequest *request = [NSURLRequest requestWithURL:
                             [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/json?location=-15.815347,-47.9164097&radius=500&types=restaurant&sensor=true&key=AIzaSyBLY-lBALViJ6ybrgtOqQGhsCDQtsdKsnc"]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];



    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

#pragma mark NSURLConnection delegate methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [connection release];
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    [responseData release];

    NSDictionary *results = [responseString JSONValue];

    NSMutableArray *allTweets = [results objectForKey:@"results"];

    //This is the part that I get an ERROR
    [viewController setTweets:allTweets];
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
}

SimpleSplitController.h

#import <UIKit/UIKit.h>
#import "APSplitViewController.h"
#import <MapKit/MapKit.h>

@interface SimpleSplitController : APSplitViewController {
    NSMutableData *responseData;
    NSArray *tweets;
}

@property (nonatomic, retain) UIViewController *left;
@property (nonatomic, retain) UIViewController *right;
@property (nonatomic, retain) NSArray *tweets;

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;

@end

SimpleSplitController.m

#import "SimpleSplitController.h"
#import <QuartzCore/QuartzCore.h>
#import "JSON/JSON.h"
#import "Tweet.h"

@interface SimpleSplitController()
- (UIColor *) randomColor;
- (UIViewController*) randomViewController1;
- (UIViewController*) randomViewController2;
- (UIViewController*) randomViewController3;
- (void) buttonPushRandomViewController1;
- (void) buttonPushRandomViewController2;

@end

@implementation SimpleSplitController

@synthesize left, right;

@synthesize tweets;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

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

    // Configure the cell...

    NSDictionary *aTweet = [tweets objectAtIndex:[indexPath row]];

    cell.textLabel.text = [aTweet objectForKey:@"name"];
    cell.textLabel.adjustsFontSizeToFitWidth = YES;
    cell.textLabel.font = [UIFont systemFontOfSize:12];
    cell.textLabel.minimumFontSize = 10;
    cell.textLabel.numberOfLines = 4;
    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;

    cell.detailTextLabel.text = [aTweet objectForKey:@"vicinity"];

    NSURL *url = [NSURL URLWithString:[aTweet objectForKey:@"icon"]];
    NSData *data = [NSData dataWithContentsOfURL:url];
    cell.imageView.image = [UIImage imageWithData:data];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;

}

Good evening!

I need some help. I'm getting some data from the Google Places API in JSON format but I'm not getting to populate a TableView in iPad (SplitView Based Application). I starting with iOS developing so probably there are many mistakes! I used a project example that used Twitter API to retrieve the posts and just renamed the JSON data names.

I have four files that are used in the project and implement the function:

SimpleSplitController.h
SimpleSplitController.m
SplitSampleAppDelegate.h
SplitSampleAppDelegate.m

I get an ERROR at SplitSampleDelegate.m file, as it's checked there...

If someone may help me, I'd be very grateful!

Here are the codes that implement:

SplitSampleAppDelegate.h

#import <UIKit/UIKit.h>

@class APTabBarControllerForSplitController;

@interface SplitSampleAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {

    NSMutableData *responseData;
    NSMutableArray *tweets;

}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) IBOutlet APTabBarControllerForSplitController *tabBarController;

@property (nonatomic, retain) NSMutableArray *tweets;

@end

SplitSampleAppDelegate.m

#import "SplitSampleAppDelegate.h"
#import "APTabBarControllerForSplitController.h"

@implementation SplitSampleAppDelegate

@synthesize window=_window;

@synthesize tabBarController=_tabBarController;

@synthesize tweets;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.tabBarController.delegate = self;

    // Override point for customization after application launch.
    // Add the tab bar controller's current view as a subview of the window
    // Add the view controller's view to the window and display.
    responseData = [[NSMutableData data] retain];
    tweets = [NSMutableArray array];
    NSURLRequest *request = [NSURLRequest requestWithURL:
                             [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/json?location=-15.815347,-47.9164097&radius=500&types=restaurant&sensor=true&key=AIzaSyBLY-lBALViJ6ybrgtOqQGhsCDQtsdKsnc"]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];



    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

#pragma mark NSURLConnection delegate methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [connection release];
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    [responseData release];

    NSDictionary *results = [responseString JSONValue];

    NSMutableArray *allTweets = [results objectForKey:@"results"];

    //This is the part that I get an ERROR
    [viewController setTweets:allTweets];
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
}

SimpleSplitController.h

#import <UIKit/UIKit.h>
#import "APSplitViewController.h"
#import <MapKit/MapKit.h>

@interface SimpleSplitController : APSplitViewController {
    NSMutableData *responseData;
    NSArray *tweets;
}

@property (nonatomic, retain) UIViewController *left;
@property (nonatomic, retain) UIViewController *right;
@property (nonatomic, retain) NSArray *tweets;

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;

@end

SimpleSplitController.m

#import "SimpleSplitController.h"
#import <QuartzCore/QuartzCore.h>
#import "JSON/JSON.h"
#import "Tweet.h"

@interface SimpleSplitController()
- (UIColor *) randomColor;
- (UIViewController*) randomViewController1;
- (UIViewController*) randomViewController2;
- (UIViewController*) randomViewController3;
- (void) buttonPushRandomViewController1;
- (void) buttonPushRandomViewController2;

@end

@implementation SimpleSplitController

@synthesize left, right;

@synthesize tweets;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

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

    // Configure the cell...

    NSDictionary *aTweet = [tweets objectAtIndex:[indexPath row]];

    cell.textLabel.text = [aTweet objectForKey:@"name"];
    cell.textLabel.adjustsFontSizeToFitWidth = YES;
    cell.textLabel.font = [UIFont systemFontOfSize:12];
    cell.textLabel.minimumFontSize = 10;
    cell.textLabel.numberOfLines = 4;
    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;

    cell.detailTextLabel.text = [aTweet objectForKey:@"vicinity"];

    NSURL *url = [NSURL URLWithString:[aTweet objectForKey:@"icon"]];
    NSData *data = [NSData dataWithContentsOfURL:url];
    cell.imageView.image = [UIImage imageWithData:data];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰火雁神 2024-12-16 07:08:22

很可能你在这里错了。您正在检查对象,而不是值。

 NSMutableArray *allTweets = [results valueForKey:@"results"];

乔普你的问题已经解决了。

Probable you are wrong here. You are checking object, instead of value.

 NSMutableArray *allTweets = [results valueForKey:@"results"];

Jope your problem is resolved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文