Iphone 将json数据添加到UITableView中
这是我的viewController.m文件中的一个示例getData方法Iphone 将json数据添加到UITableView中,iphone,Iphone,这是我的viewController.m文件中的一个示例getData方法 -(void) getData { // Create new SBJSON parser object SBJsonParser *parser = [[SBJsonParser alloc] init]; // Prepare URL request to download statuses from Twitter NSURLRequest *request = [NSURLRequest requestWith
-(void) getData {
// Create new SBJSON parser object
SBJsonParser *parser = [[SBJsonParser alloc] init];
// Prepare URL request to download statuses from Twitter
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://twitter.com/statuses/public_timeline.json"]];
// Perform request and get JSON back as a NSData object
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// Get JSON as a NSString from NSData response
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
// parse the JSON response into an object
// Here we're using NSArray since we're parsing an array of JSON status objects
NSArray *statuses = [parser objectWithString:json_string error:nil];
// Each element in statuses is a single status
// represented as a NSDictionary
for (NSDictionary *status in statuses)
{
NSString *text = [status objectForKey:@"text"];
// You can retrieve individual values using objectForKey on the status NSDictionary
// This will print the tweet and username to the console
NSLog(@"%@ - %@", [status objectForKey:@"text"], [[status objectForKey:@"user"] objectForKey:@"screen_name"]);
}
}
我想做的是使用[[status objectForKey:@“user”]
并使其成为表视图中单元格的名称,我将如何执行此操作
编辑:好的,我把它放入一个字符串中,但现在我尝试加载它时,它崩溃了,说[\uu NSCFDictionary isEqualToString:]:未识别的选择器发送到实例0x6892b60'
它在cell.textLabel.text=[[statusArray objectAtIndex:indexPath.row]objectForKey:@“user”];
说线程1 SIGABRT
实现协议:
-返回“状态”的编号表格视图:numberOfRowsInSection:
-返回一个UITableViewCell,其textLabel.text设置为您的文本tableView:cellForRowAtIndexPath:
苹果的文档中有很多例子,包括使用Xcode的“Master Detail app”模板创建新应用程序时得到的项目。在getData函数中,将所有数据收集到一个数组中。比如说statusArray。 现在,从UItableViewController派生了一个viewcontroller。将此类中的一个成员设为NSArray类型,并将上面的数组分配给它。 将以下函数写入控制器类
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return statusArray.count;
}
- (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];
}
cell.textLabel.text = [statusArray objectAtIndex:indexPath.row] objectForKey:@"user"];
return cell;
}
仅供参考,
numberOfSectionsInTableView:
默认情况下在UITableViewController
中返回1,这样子类中就不需要它了对不起,我是objective-c的新手。我如何准确地将其收集到一个数组中?因为我尝试了创建一个实例变量(可能做得不对)statusArray的NSArray*status=[parser objectWithString:json_string error:nil];我确信这是错误的,因为它在我的表视图中没有显示任何内容。