gpt4 book ai didi

ios - connectionDidFinishLoading 完成后无法重新加载 UITableView

转载 作者:行者123 更新时间:2023-11-28 20:39:57 25 4
gpt4 key购买 nike

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

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

}

谢谢,跳蚤

最佳答案

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

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

关于ios - connectionDidFinishLoading 完成后无法重新加载 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9040631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com