簡體   English   中英

代碼使用xib,但沒有使用Storyboard

[英]Code working with xib, but not with Storyboard

我是iPhone應用程序的新手。 我正在學習如何獲取JSON數據並解析它並在iPhone中的表格中顯示它。 為此,我將瀏覽this video

我用xib文件做了這個並且它工作得很好。

我正在嘗試用Storyboard做到這一點。 我做了與Stoaryboard相同的事情,但執行是在下面的線上休息。

cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"title"];

我的代碼是

-(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"i m here 1");

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
    NSLog(@"i m here 2");

    if (cell==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];
        NSLog(@"i m here 3");
    }
    NSLog(@"i m here 4");
    cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"title"];
    // cell.textLabel.text = @"dadsf ad";
    NSLog(@"i m here 5");
    return cell;
}

NSLog ,我得到輸出直到im here 4

當我評論cell.textLabel.text = [[news objectAtIndex並取消注釋cell.textLabel.text = @"dadsf ad"; ,它有效,我在iPhone中看到了dadsf ad

// cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"title"];
cell.textLabel.text = @"dadsf ad";

當我使用cell.textLabel.text = [[news objectAt ,我得到屏幕以下。

在此輸入圖像描述

注意:

我也去了模擬器並做了Reset Content and Setting ,仍然面臨同樣的問題。 知道為什么嗎?


更新1

我激活NSZombieEnabled並運行代碼。 我進入NSLOG了

JSONNewsStoryBoard[19389:11303] i m here 4
2013-01-06 11:26:15.327 JSONNewsStoryBoard[19389:11303] *** -[CFArray objectAtIndex:]: message sent to deallocated instance 0x7497040

以下是截圖。

在此輸入圖像描述


更新2 - 答案

我嘗試添加NSLog(@"checking news count %d", [news count]); cellForRowAtIndexPath ,我遇到了同樣的問題。 意味着news無效。

所以我補充道

news = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];

cellForRowAtIndexPath它起作用了。 :) :)

看來,在調用objectAtIndex:之前, news對象已被解除分配,這可能與界面構建器和故事板接管對你提供的對象的控制方式有關。 嘗試檢查代碼中各個點的news是否等於nil 您可能必須在不同的時間點實例化news

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM