【问题标题】:Unable to reload UITableView after connectionDidFinishLoading completesconnectionDidFinishLoading 完成后无法重新加载 UITableView
【发布时间】:2012-01-27 21:59:24
【问题描述】:

我正在下载和解析 JSON 对象以构建“新闻提要”来填充 UITableView。我在 connectionDidFinishLoading 委托方法中的最后一行代码是:

[tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];

但是,我在 - (UITableViewCell *)tableView:(UITableView *)mytableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法中的断点没有被命中。 (它们在应用首次启动时被击中)

所以无论出于何种原因,即使我在主线程上调用 reloadData;它似乎没有开火。我只尝试了 [tableView reloadData],但没有奏效。

这是我的 connectionDidFinishLoading 方法:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{       

    //NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

    NSArray *publicTimeline = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil]; 

    NSUInteger newsStreamCount = [publicTimeline count];

    // Initialize the collection and the dictionary
    newsItemManager.newsItemCollection = [[NSMutableArray alloc] initWithCapacity:newsStreamCount];
    NSMutableArray *dictOfNewsItems = [[NSMutableArray alloc] initWithCapacity:newsStreamCount];

    // From the JSON object, parse out the individual news item JSON objects and 
    // put them into a dictionary.
    for (int i = 0; i < newsStreamCount; i++)
    {
        NSDictionary *item = [publicTimeline objectAtIndex:i];
        [dictOfNewsItems addObject:item];
    }

    // For each news item JSON object, extract out the information we need for our
    // news manager class
    for (int i = 0; i < newsStreamCount; i++)
    {
        NSString *userName = [[dictOfNewsItems objectAtIndex:i] valueForKey:@"Title"];
        NSString *message = [[dictOfNewsItems objectAtIndex:i] valueForKey:@"Content"];
        NSString *imgUrl = [[dictOfNewsItems objectAtIndex:i] valueForKey:@"https://si0.twimg.com/logo_normal.jpg"];


        NewsItem *newsItem = [[NewsItem alloc] initWithBasicInfo:userName :message :imgUrl];


        [newsItemManager.newsItemCollection addObject:newsItem];
    }

    [tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}

这是我的头文件:

#import <UIKit/UIKit.h>

@interface NewsViewController : UITableViewController

@property (nonatomic, retain) NSMutableArray* newsItemArray;
@property (nonatomic, retain) NSMutableData *responseData;
@property (nonatomic, retain) IBOutlet UITableView *tableView;

@end

这是我的实现:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    self.title = @"News";

    //self.newsItemArray = [[NSMutableArray alloc] initWithObjects:@"One", @"Two", @"Three", nil];

    tableView.rowHeight = 75.0;


    responseData = [NSMutableData data];        
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://blah.com/api/news"]];
    (void)[[NSURLConnection alloc] initWithRequest:request delegate:self];

}

谢谢, 跳蚤

【问题讨论】:

  • 在您调用 performSelector 时,tableView 是否由于某种原因为零? NSLog(@" Tableview is %@", tableView);
  • 是的,Ray,看起来确实是这样:打印出来的是:Tableview is (null)
  • 我不知道为什么会这样;我是全班第一的@synthesize。
  • 我认为问题一定是因为这个 UITableView 指针为空,但我不确定为什么会这样。

标签: ios uitableview xcode4.2


【解决方案1】:

如果你的 connectionDidFinish... 方法在你的 tableViewController 类中,也许你只需要

[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];

【讨论】:

  • 请在声明 tableview 的地方发布你的 .h 文件,以及你的 init 方法(或者你初始化 tableview 的任何地方)。该表是否显示数据?
  • 嘿,Ray,我更新了我的主要问题并将我的标题信息和实现信息放在那里。在使用 connectionDidFinish 方法之外,我能够让表格显示一个简单数组的内容。
  • tableView的dataSource在哪里设置?应该是 newsItemManager.newsItemCollection...
【解决方案2】:

总体问题是我的 newsItemManager.newsItemCollection 没有被正确初始化并且一直返回 null,因此当 UITableView 试图加载数据时;没有可加载的内容。

我以为我已经检查过了,但其中一个问题是整天盯着电脑看,错过了明显的东西。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-08
    • 1970-01-01
    • 1970-01-01
    • 2011-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多