【发布时间】:2010-04-08 10:47:58
【问题描述】:
是的,我已经四处搜索,找不到任何东西。
@synthesize list; // this is an NSArry
-(void) viewDidLoad {
NSArray *arr = [self getJSONFeed];
self.List = [arr retain]; // if i copy the access code into here it works fine.
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
NSArray *vals = [list objectAtIndex:row] retain];
NSString *id = [vals valueForKey:@"id"]; // ERROR
}
是的,我已经采取了一些代码来尝试并尽可能简单地提供它,忽略拼写错误和内存泄漏,这是排序的。
基本上,当我选择一行时,我无法从“列表”数组对象中获取数据。 请问谁能帮帮我?
这是我的完整代码
我使用http://code.google.com/p/json-framework/ 连接我的JSON
我的 Json 以这种格式返回:
[
{
"id": "7812",
"title": "title1",
"fulltext": "main body text",
"created": "2010-04-06 11:30:03"
},
{
"id": "7813",
"title": "title2",
"fulltext": "main body text2",
"created": "2010-04-06 11:30:03"
}
]
MainNewsController.h
在这里声明:
NSArray *list:
@property (nonatomic, retain) NSArray *list;
MainNewsController.m
@synthesize list;
-(void) viewDidLoad {
NSArray *array = [[NSArray alloc] initWithArray: [self downloadPublicJaikuFeed]];
self.list = array;
NSArray *stream = [list objectAtIndex:6]; // can change index and it's fine
NSString *vall = [stream valueForKey:@"title"]; // works fine
NSString *val1l = [stream valueForKey:@"id"]; // works fine
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.list count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MainNewsCellIdentifier = @"MainNewsCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: MainNewsCellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: MainNewsCellIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
NSDictionary *stream = (NSDictionary *) [list objectAtIndex:row];
cell.textLabel.text = [stream valueForKey:@"id"];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
[stream release];
return cell;
}
#pragma mark -
#pragma mark Table Delegate Methods
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (childController == nil)
childController = [[MainNewsDetailController alloc] initWithNibName:@"DisclosureDetail" bundle:nil];
childController.title = @"News Detail Button Pressed";
NSUInteger row = [indexPath row];
NSArray *stream = (NSArray *) [self.list objectAtIndex:row];
NSString *vall = [stream valueForKey:@"title"]; // error
NSString *val1l = [stream valueForKey:@"id"]; // error
childController.message = @"111";
childController.title = @"222";
[self.navigationController pushViewController:childController animated:YES];
}
【问题讨论】:
-
您遇到的错误是什么?另外,“将访问代码复制到此处”是什么意思,您的意思是
[arr copy];? -
通过访问代码,我的意思是如果我从 didSelectRowAtIndexPath 中取出三行并将它们放在 viewDidLoad 的最后一行下面,我就能够提取数据。但是在 didSelectRowAtIndexPath 中它会出错。我得到的错误是 EXC_BAD_ACCESS
标签: iphone ios objective-c cocoa-touch nsarray