【发布时间】: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