【问题标题】:iOS: Slide to display data after reloading UITableViewiOS:重新加载 UITableView 后滑动显示数据
【发布时间】:2014-08-06 15:51:29
【问题描述】:

以下是我的应用如何将数据加载到 UITableView 中的顺序:

  1. UITableView 从空数组加载。
  2. 应用异步获取数据。
  3. 获取并处理数据后,应用调用 UITableView reloadData。

我目前遇到的问题是,我需要手动稍微滑动一下 UITableView,才能让 UITableView 显示获取到的数据。

目前我正在使用iOS模拟器进行测试。

这被认为是正常的吗?在没有用户请求滑动数据的情况下,我可以做些什么来显示数据?

-- 编辑--

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *tableCellIdentifier = @"TempTableViewCell";

    TempTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableCellIdentifier forIndexPath:indexPath];

    int row = [indexPath row];
    YoutubeVideo *currentVideo = [videoArray objectAtIndex:row];

    cell.TempLabel.text = currentVideo.VideoTitle;
    [cell.TempImage setImageWithURL:[NSURL URLWithString:currentVideo.VideoThumbnailURL] placeholderImage:[UIImage imageNamed:@"Icon-Small"]];
    cell.TempLabelDate.text = currentVideo.VideoDate;
    return cell;
}

【问题讨论】:

  • 您的滑动是否会导致单元格的重复使用?换句话说,在你滑动的时候有没有被替换的单元格?
  • 我已经为 cellForRowAtIndexPath 添加了代码。
  • 你可以尝试在tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath中设置图片和文字吗?
  • 还是一样。你以前遇到过这样的事情吗?
  • 我想你可以按照这里给出的解决方案stackoverflow.com/questions/2770158/…

标签: objective-c uitableview xcode5


【解决方案1】:

如果我正确理解了您的问题,那么...

我猜当你调用[tableView reloadData] 时,你不在主线程上。试试这个:

dispatch_async(dispatch_get_main_queue(), ^{
    [tableView reloadData];
});

编辑说明:

原因是您的网络请求可能发生在不同的线程上,一旦您完成并重新加载表,您仍处于此后台线程上。执行 UI 更新时,您需要在主线程上执行此操作,否则会发生奇怪的事情,就像您在此处看到的那样。

上面的代码将在主线程上执行表更新。

【讨论】:

  • 太棒了..它现在可以工作了..与android真的不同...看起来我会遇到各种奇怪的东西..:P
  • 酷 - 很高兴我能帮上忙。
猜你喜欢
  • 2013-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-29
  • 2015-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多