在UITableView中显示Json结果

时间:2013-04-22 14:55:22

标签: ios objective-c json uitableview

我正在尝试将JSON个结果放入我的UITableView。我得到了JSON,但我无法将它们放到我的桌面视图中。知道我做错了吗?

以下是一些相关代码:

- (void)viewDidLoad {
    TitlosArray = [[NSMutableArray alloc] init];
    KeimenoArray = [[NSMutableArray alloc] init];
    [super viewDidLoad];
    [self fetchTweets];
}

我的JSON解析器工作正常:

- (void)fetchTweets {
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    dispatch_async(queue,  ^{
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://myselection.gr/support3/frontistiria_project/android_data.php/?type=publicNews"]];

        [self performSelectorOnMainThread:@selector(fetchedForecast:)withObject:data waitUntilDone:YES];
    });
}

- (void)fetchedForecast:(NSData *)responseData {
    NSError *error;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

    NSLog(@"Rows %@", [json objectForKey:@"total_rows"]);
    NSArray *items = [json valueForKeyPath:@"content"];

    NSEnumerator *enumerator = [items objectEnumerator];
    titlosjson.text=[[items objectAtIndex:1] objectForKey:@"title"];
    Keimenojson.text=[[items objectAtIndex:1] objectForKey:@"descr"];

    while (item = (NSDictionary*)[enumerator nextObject]) {
        [TitlosArray addObject:[item objectForKey:@"title"]];
        [KeimenoArray addObject:[item objectForKey:@"descr"]];
        NSLog(@"Title = %@", [item objectForKey:@"title"]);
        NSLog(@"Descr = %@",[item objectForKey:@"descr"]);
    }

    NSLog(@"%@",[TitlosArray objectAtIndex: 1]);
    myArray = [NSArray arrayWithArray:TitlosArray ];
    NSLog(@"%@", [myArray objectAtIndex: 2]);
}

但我的UITableView

有问题
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return myArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"TweetCell";

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

    //cell.textLabel.text = [NSString stringWithFormat:@"by %@", text];
    return cell;
}

3 个答案:

答案 0 :(得分:6)

您正在创建空单元格,因此不会显示您的内容。

尝试使用以下方法替换cellForRowAtIndexPath:方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"TweetCell";

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

    cell.textLabel.text = [TitlosArray objectAtIndex:indexPath.row];

    return cell;
}

答案 1 :(得分:4)

这是有效的,经过测试。

由示例单一视图应用制作,在IB中添加了tableView(并连接到IBOutlet

#import "ViewController.h"

@interface ViewController ()
{
    NSMutableArray *titlosArray;
    NSMutableArray *keimenoArray;
}

@property (strong) IBOutlet UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    titlosArray = [[NSMutableArray alloc] init];
    keimenoArray = [[NSMutableArray alloc] init];
    [super viewDidLoad];
    [self fetchTweets];
}

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

- (void)fetchTweets
{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    dispatch_async(queue,  ^{
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://myselection.gr/support3/frontistiria_project/android_data.php/?type=publicNews"]];

        [self performSelectorOnMainThread:@selector(fetchedForecast:)withObject:data waitUntilDone:YES];
    });
}

- (void)fetchedForecast:(NSData *)responseData
{
    NSError *error;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

    NSLog(@"Rows %@", [json objectForKey:@"total_rows"]);

    NSArray *items = [json valueForKeyPath:@"content"];

    //clear the arrays (if you're planning to download data more then once - refreshing...)

    [titlosArray removeAllObjects];
    [keimenoArray removeAllObjects];

    for (NSDictionary *item in items)
    {
        [titlosArray addObject:[item objectForKey:@"title"]];
        [keimenoArray addObject:[item objectForKey:@"descr"]];
    }

    //once you updated youd data-model you have to reload it
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return titlosArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"TweetCell";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = (NSString *)[titlosArray objectAtIndex:indexPath.row];

    return cell;
}

@end

enter image description here

修改

如果您还想查看副标题中的详细信息,可以将cellForRowAtIndexPath:更改为:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"TweetCell";

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

    cell.textLabel.text = (NSString *)[titlosArray objectAtIndex:indexPath.row];

    cell.detailTextLabel.text=(NSString *)[keimenoArray objectAtIndex:indexPath.row];

    return cell;
}

enter image description here

请注意,如果您想要显示完整的文本(因为您的查询会返回每条记录的大量文本),您将不得不摆弄自定义UITableViewCell实现。

您可能会发现本教程很有趣:Table View Animations and Gestures

答案 2 :(得分:1)

我在这里改变您的代码,(希望您不介意)尝试用以下代码替换您的代码:

- (void)fetchedForecast:(NSData *)responseData {
    NSError *error;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

    NSLog(@"Rows %@", [json objectForKey:@"total_rows"]);
    NSArray *items = [json valueForKeyPath:@"content"];

    NSEnumerator *enumerator = [items objectEnumerator];
    titlosjson.text=[[items objectAtIndex:1] objectForKey:@"title"];
    Keimenojson.text=[[items objectAtIndex:1] objectForKey:@"descr"];
    myArray = [[NSMutableArray alloc] initWithCapacity:0];
    while (NSDictionary *item = (NSDictionary*)[enumerator nextObject]) {
        NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                    [item objectForKey:@"title"], @"title",
                                    [item objectForKey:@"descr"], @"descr", nil];
        [myArray addObject:dictionary];
    }
}
//But I have problem with my UITableView:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return myArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"TweetCell";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    NSArray *myArray;
    cell.textLabel.text = [[myArray objectAtIndex:indexPath.row] valueForKey:@"title"];
    cell.detailTextLabel.text = [[myArray objectAtIndex:indexPath.row] valueForKey:@"descr"];

    return cell;
}

以下是要点:

  • cellForRowAtIndexPath方法中,您没有为titleLabel
  • 设置任何值
  • 我已将cellStyle设为UITableViewCellStyleSubtitle,可以设置详细说明(如果您不喜欢,可以更改)
  • 最后我没有使用两个不同的数组,而是使用了你的myArray(使其成为可变的)并在其中添加了字典,然后在为您的单元格设置值时使用了这些值。