简体   繁体   English

connectionDidFinishLoading完成后无法重新加载UITableView

[英]Unable to reload UITableView after connectionDidFinishLoading completes

I am downloading and parsing JSON objects to build a "news feed" to populate a UITableView. 我正在下载和解析JSON对象,以构建“新闻提要”来填充UITableView。 The very last line of code I have in the connectionDidFinishLoading delegate method is: 我在connectionDidFinishLoading委托方法中拥有的最后一行代码是:

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

However, my break points in the - (UITableViewCell *)tableView:(UITableView *)mytableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method are not hit. 但是,没有击中-(UITableViewCell *)tableView:(UITableView *)mytableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法的断点。 (They are hit when the app first launches) (在应用首次启动时会被点击)

So for whatever reason even though I am calling the reloadData on the main thread; 所以无论出于什么原因,即使我在主线程上调用reloadData; it doesn't appear to be firing. 它似乎没有开火。 I tried just [tableView reloadData] and that did not work. 我只尝试了[tableView reloadData],但没有成功。

Here is my connectionDidFinishLoading method: 这是我的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];
}

Here is my header file: 这是我的头文件:

#import <UIKit/UIKit.h>

@interface NewsViewController : UITableViewController

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

@end

Here is my implementation: 这是我的实现:

- (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];

}

Thanks, Flea 谢谢,跳蚤

如果您的connectionDidFinish ...方法位于tableViewController类中,则可能只需要

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

The overall problem was my newsItemManager.newsItemCollection was not being initialized properly and was returning null the entire time, thus when the UITableView was trying to load data; 总体问题是我的newsItemManager.newsItemCollection没有正确初始化,并且在整个时间(因此,当UITableView尝试加载数据时)都返回null。 there was nothing to load. 没有什么可加载的。

I thought I had checked for this but one of those problems of staring at the computer all day and missing the obvious. 我以为我已经检查过了,但是整天盯着电脑却没有明显的问题之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM