将json数据添加到UITableView

将json数据添加到UITableView

本文介绍了将json数据添加到UITableView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的viewController.m文件中的示例getData方法

This is a sample getData method in my viewController.m file

-(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] 并使其成为表视图中单元格的名称,我将如何进行此操作?

What i want to do is take the [[status objectForKey:@"user"]and make make it the name of the cell in the table view, How would i go about doing this?

编辑:好的所以我把它变成了一个字符串但现在我尝试加载它崩溃说 [__ NSCFDictionary isEqualToString:]:无法识别的选择器发送到实例0x6892b60 '

Okay so i got it into a string but now that i try to load it it crashes saying [__NSCFDictionary isEqualToString:]: unrecognized selector sent to instance 0x6892b60'

它显示旁边的线程cell.textLabel.text = [[statusArray objectAtIndex:indexPath。 row] objectForKey:@user]; 线程1 SIGABRT

推荐答案

在getData函数中收集数组中的所有数据。说它statusArray。
现在,从UItableViewController派生一个viewcontroller。在此类NSArray类型中创建一个成员,并为其分配上面的数组。
将以下函数写入控制器类。

In getData function gather all the data in an array. say it statusArray.Now, derived a viewcontroller from UItableViewController. Make a member in this class of NSArray type and assign above array to it.Write below functions into the controller class.

- (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;
}

这篇关于将json数据添加到UITableView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 08:25