[英]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
,仍然面臨同樣的問題。 知道為什么嗎?
我激活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
以下是截圖。
我嘗試添加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.