0

我试图把我的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;
}
4

3 回答 3

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;
}
于 2013-04-22T15:16:22.803 回答
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

在此处输入图像描述

编辑:

如果您还想查看字幕中的详细信息,可以更改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;
}

在此处输入图像描述

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

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

于 2013-04-22T17:35:27.333 回答
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(使该数组可变)并在其中添加了字典,然后在为单元格设置值时使用了这些值。
于 2013-04-22T15:36:56.243 回答