【问题标题】:TableView reloadData doesn't update UI in iPad , but works in iPhoneTableView reloadData 不会更新 iPad 中的 UI,但适用于 iPhone
【发布时间】:2016-09-10 10:52:43
【问题描述】:

我有一个奇怪的错误。我正在使用UITableView,当一行被选中时必须重新加载。这在 iPhone 模拟器中运行良好。在 iPad 上运行时,didSelectRowAtIndexPath 被调用,但 UI 没有得到更新。 我尝试在主线程上调用方法reloadData。还是一样。

我在 stackoverflow 中尝试了各种解决方案。似乎没有什么可以解决我的问题。

更新

当我选择一个单元格时,将添加新的单元格。所以我需要重新加载我的 tableview 来显示动态变化。 在下面发布我的代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
NSUInteger count=indexPath.row+1;

NSMutableArray *insertIndexPaths = [NSMutableArray array];

[insertIndexPaths addObject:[NSIndexPath indexPathForRow:count inSection:indexPath.section]];

switch (indexPath.row) {
        case 0:
    {
    if ([ItemsInSection[indexPath.section] count]==1){
                [ItemsInSection[indexPath.section] addObject:obj2];

                [TabView beginUpdates];
                [TabView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationLeft];

                [TabView endUpdates];        
            }
            else {

                [ItemsInSection[indexPath.section] removeAllObjects];
                [ItemsInSection[indexPath.section] addObject:obj1];

                [self.TabView reloadData];

            }
        }
            break;

        case 1:

    {if ([ItemsInSection[indexPath.section] count]==2){
                [ItemsInSection[indexPath.section] addObject:obj3];

                [self.TabView beginUpdates];
                [TabView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationLeft];
                [self.TabView endUpdates];

            }
            else
            {
                [ItemsInSection[indexPath.section] removeAllObjects];
                [ItemsInSection[indexPath.section] addObject:obj1];
                [ItemsInSection[indexPath.section] addObject:obj2];

                [self.TabView reloadData];
              }
        }
            break;

        case 2: {

            if ([ItemsInSection[indexPath.section] count]==3) {
                 [ItemsInSection[indexPath.section] addObject:obj4];
                [self.TabView beginUpdates];
                 [TabView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationLeft];
                [self.TabView endUpdates];

            }
            else
            {
                [ItemsInSection[indexPath.section] removeObject:obj4];
                [self.TabView beginUpdates];
                 [TabView deleteRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationLeft];

                [self.TabView endUpdates];

            }
          }
            break;

                  default:
            break;
    }

}

【问题讨论】:

  • 您应该发布您的代码。在我看来,问题出在其他地方。此外,为什么要在选择单元格时重新加载整个表格视图?请描述您尝试实现的实际目标,而不是为您的失败尝试寻找解决方案。

标签: ios objective-c uitableview ipad xcode7.2


【解决方案1】:

您正在表视图委托中重新加载表视图,在表视图选择委托方法上有它们相关的选择动画过程。因此,我建议您将此方法中的所有代码移动到单独的方法中。并调用该方法。

   dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(),^{
          [self.aiTableView beginUpdates];
          // your table view selection code
          [self.aiTableView endUpdates];
     });  

【讨论】:

  • 这不能解决我的问题。 NumberOfRowsInSection 方法返回 no.of rows 。但它没有被插入 UITableView 。这只发生在 iPad 中。我的原始代码在 iPhone 中运行良好。我想知道可能是什么问题
  • 请尝试将我的所有代码从 dispatch_after() 移动到 dispatch_async(dispatch_get_main_queue(), ^{ }); ,请确保您在调用此代码块时超出了表视图委托选择方法。
  • 我不确定您的代码在 iPhone 和 iPad 上的行为。 :)
【解决方案2】:

我已经找到了这个问题的解决方案。 UITableViewCell 的背景色设置为透明色,UITableViewCell 上的UIView 的背景色也设置为透明色。更改这些设置会显示 iPad 模拟器中 UITableView 中的行插入。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-16
    相关资源
    最近更新 更多