2013-04-22 111 views
0

我試圖將我的JSON結果放入我的UITableView中。我得到了JSON,但我不能把它們放到我的tableview中。任何想法我做錯了什麼?在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

嗨,歡迎來到SO。您需要添加更多關於正在工作和未正常工作的詳細信息。問題是什麼 ? 「我有一個問題」不足以幫助我們,對不起。 – rdurand 2013-04-22 15:04:28

+0

我的json完美無缺。我將數據存儲在MutableArray中。當我嘗試在我的tableView中添加我的數組時,我從不顯示它們。我想在tableView中添加「TitlosArray」。最後的程序comile完美沒有錯誤:( – Cliff 2013-04-22 15:08:10

+0

檢查rdurands的答案並添加'[self.tableview reloadData];'作爲最後一行的 - (void)fetchedForecast:(NSData *)responseData' – 2013-04-22 17:17:04

回答

4

這工作,測試。

從樣品單視圖應用進行的,的tableView在IB加入(並連接到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

+1

Ty我會試試:) – Cliff 2013-04-22 19:14:18

+0

不客氣!如果遇到任何問題,請留下評論。 – 2013-04-22 19:30:58

+0

我還有1個問題。我正在嘗試添加Subtitle,我將Storyboard上的Table設置爲Custom。我添加代碼:cell.detailTextLabel.text =(NSString *)[keimenoArray objectAtIndex:indexPath.row];我不能看到的Subtible。 – Cliff 2013-04-22 19:54:40

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

我在這裏改變你的代碼,(希望你不會介意)嘗試用這種替代代碼:

- (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
  • 我使你的cellStyleUITableViewCellStyleSubtitle,將能夠設定詳細的描述(你可以改變它,如果你不喜歡它)
  • 最後,而不是採取兩種不同的陣列我用您的myArray(發可變的)並在其中添加字典,然後使用這些值,同時將值設置到您的單元格。
+0

「],@」title「, [item objectForKey:@」descr「],@」descr「,nil]; [myArray addObject:dictionary]; NSLog(@」%@「,myArray [0]); }問題在這裏。我所有的結果都在myArray表的第0行表 – Cliff 2013-04-22 16:27:56

+0

你的意思是,你沒有得到任何記錄? – rptwsthi 2013-04-22 16:32:54