Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 未在UITableView中加载数据。可能是什么问题?_Iphone_Uitableview_Ios5_Tableview_Segue - Fatal编程技术网

Iphone 未在UITableView中加载数据。可能是什么问题?

Iphone 未在UITableView中加载数据。可能是什么问题?,iphone,uitableview,ios5,tableview,segue,Iphone,Uitableview,Ios5,Tableview,Segue,按下按钮时,我在下一个序列中有一个UITableView。我在实现文件中实现的代码如下。视图正在移动,但数据未加载到视图中 有什么问题吗 - (void)viewDidLoad { [super viewDidLoad]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSData* data = [NSData dataWithContentsOf

按下按钮时,我在下一个序列中有一个
UITableView
。我在实现文件中实现的代码如下。视图正在移动,但数据未加载到视图中

有什么问题吗

- (void)viewDidLoad
{
    [super viewDidLoad];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSData* data = [NSData dataWithContentsOfURL:
                        [NSURL URLWithString: @"https://api.twitter.com/1/statuses/public_timeline.json"]];
        NSError* error;
        tweets = [NSJSONSerialization JSONObjectWithData:data
                                                 options:kNilOptions
                                                   error:&error];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    });

    // 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;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return tweets.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"TweetCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    NSDictionary *tweet = [tweets objectAtIndex:indexPath.row];
    NSString *text = [tweet objectForKey:@"text"];
    NSString *name = [[tweet objectForKey:@"user"] objectForKey:@"name"];
    cell.textLabel.text = text;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"by %@", name];
    return cell;
}

您应该在调试器中使用断点来中断
numberofrowsinssection
方法和
cellforrowatinexpath
以验证数据是否被正确解析和处理。如果您不熟悉断点,现在是了解它们的好机会——它们在调试和解决问题方面是非常宝贵的

一般来说,您可能不应该使用
dataWithContentsOfURL
来进行联网。它不是专门为这种用途设计的
NSURLConnection
是一个更好的选择,它将为您提供更大的灵活性

(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 0;
}
您不需要至少一节吗?

dyspatch_async
被命名为,因为它是异步的。您似乎认为对它的第二次调用是在第一次调用之后执行的,但不是。它们是并行执行的,而且因为URL操作较慢,所以表视图的更新在没有数据时发生

解决方案:将reloadData调用与URL操作放在同一块中-不需要两个单独的调用来分派\u async

此外,您还可以从中返回0

numberOfSectionsInTableView:

因此,该表甚至不查找数据。返回1。

是否设置了tableView的
委托
数据源
?是否检查了接收到的JSON是否正确?NSLog(@“%@”,[[NSString alloc]initWithData:数据编码:NSUTF8StringEncoding]自动释放];是的,在另一个示例项目中,委托和数据源都被设置为TableView,相同的代码工作得非常好。因此,我假设接收到的JSON是正确的。稍后我可能需要。但现在,这只是为了测试目的。@monismanzooranthony说得很对-零段是不够的,你们确实需要一段。谢谢,伙计们。这似乎有点奏效。但数据仍然没有加载到tableview中。它现在在每个单元格中显示“by null”。那么为什么要撤销+1呢?你看到我的答案有问题吗?我没有撤销任何东西。从我的角度来看,这两个答案都是恰当的。