gpt4 book ai didi

ios - JSON 和 iPhone TableView

转载 作者:行者123 更新时间:2023-11-28 23:16:09 25 4
gpt4 key购买 nike

我是第一次尝试 JSON 解析器,我需要一些帮助当我尝试填充表格 View 时它工作正常,但是当我滚动表格或选择一行时应用程序崩溃。如果有任何帮助,我将不胜感激。

这是我的文件:

#import <UIKit/UIKit.h>
@class RootViewController;
@interface BooksJsonAppDelegate : NSObject <UIApplicationDelegate> {

UIWindow *window;
UINavigationController *navigationController;
NSMutableArray *statuses;
NSMutableData *responseData;
}
@property(nonatomic, retain)NSMutableArray *statuses;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

@end

#import "BooksJsonAppDelegate.h"
#import "RootViewController.h"
#import "SBJson.h"


@implementation BooksJsonAppDelegate

@synthesize window;
@synthesize navigationController,statuses;


#pragma mark -
#pragma mark Application lifecycle

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

SBJsonParser *parser = [[SBJsonParser alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://assignment.golgek.mobi/api/v10/items"]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
statuses = [parser objectWithString:json_string error:nil];


self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];

return YES;
}
- (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 {
NSLog(@"Connection Failed: %@",[error description]);

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
}

- (void)applicationWillResignActive:(UIApplication *)application {
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
}


- (void)applicationWillTerminate:(UIApplication *)application {
}


#pragma mark -
#pragma mark Memory management

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
}


- (void)dealloc {
[navigationController release];
[window release];
[super dealloc];
}


@end

then the root view controller

#import <UIKit/UIKit.h>
@class DetailView,BooksJsonAppDelegate;
@interface RootViewController : UITableViewController {
DetailView *detailView;
BooksJsonAppDelegate *booksAppDelegate;
}

@end

#import "RootViewController.h"
#import "DetailView.h"
#import "BooksJsonAppDelegate.h"
@implementation RootViewController

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
[super viewDidLoad];
booksAppDelegate = (BooksJsonAppDelegate *)[[UIApplication sharedApplication] delegate];

}

#pragma mark -
#pragma mark Table view data source

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [booksAppDelegate.statuses count];
}


// 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] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell.
NSDictionary *aBook = [booksAppDelegate.statuses objectAtIndex:[indexPath row]];

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

return cell;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


NSDictionary *aBook = [booksAppDelegate.statuses objectAtIndex:[indexPath row]];

detailView = [[DetailView alloc] initWithNibName:@"DetailView" bundle:nil];
// ...
// Pass the selected object to the new view controller.
detailView.title = [aBook objectForKey:@"title"];
[self.navigationController pushViewController:detailView animated:YES];
[detailView release];

}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}


- (void)dealloc {
[super dealloc];
}


@end

最佳答案

您可能需要保留您的状态

statuses = [[parser objectWithString:json_string error:nil] retain];

JSON 解析器将返回一个自动释放的对象:)


正如 Dan 在评论中指出的那样,更好的方法是像这样设置属性:

self.statuses = [parser objectWithString:json_string error:nil];

这样做的好处是如果你设置它两次就不会泄漏内存并且你可以使用 KVO 来判断它是否被改变了。好多了:)

关于ios - JSON 和 iPhone TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6305890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com