【问题标题】:Refresh array in TableView Custom Cell在 TableView 自定义单元格中刷新数组
【发布时间】:2016-02-04 08:45:18
【问题描述】:

我有一个 ViewController,它有一个使用自定义单元格生成的 TableView。每个自定义单元格都有一个 NSDictionary,它生成的数组很少。

我想找到使用[myArray replaceObjectAtIndex:... withObject:...] 替换自定义单元格中的一个数组的方法指向 TableView 中的特定单元格,前提是我已经知道 indexPath包含该数组的那个单元格。

换句话说,我必须以某种方式指出Custom Cell indexPath,到达那里并刷新数组。

有没有办法在 Objective-C 中完成这个任务?

【问题讨论】:

    标签: ios objective-c arrays uitableview


    【解决方案1】:

    如果您知道要更改的单元格的 indexPath(如果您的每个自定义单元格都不同且唯一),则可以使用 cellForRowAtIndexPath 调用获取单元格引用。

    但我强烈建议通过使用单元格位置作为参考来更改数据模型。因为表格视图重用单元格,您最终可能会得到意想不到的结果。相反,您应该更改表格模型,然后重新加载表格,或者只重新加载已更改的特定单元格。

    【讨论】:

      【解决方案2】:

      获得单元格的 indexPath 后,您可以执行以下操作:

      [self.tableView beginUpdates];
      [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPathOfYourCell, nil] withRowAnimation:UITableViewRowAnimationNone];
      [self.tableView endUpdates]; 
      

      【讨论】:

      • 问题是每个自定义单元格都有一个带有一组图像的collectionView,在第二次刷新后会失去顺序并从一个单元格跳到另一个单元格。这就是为什么我试图准确定位数组位置并更改它。
      【解决方案3】:

      您有几个选项可以重新加载UIViewController 中的UITableView

      说你的表格视图是这样的

          UITableView *tableView
      

      案例1:重新加载整个表格视图 appledoc

      [tableView reloadData];
      

      案例 2:重新加载节内的所有行 appledoc

      [tableView reloadRowsAtIndexPaths:yourIndexPath withRowAnimation: UITableViewRowAnimationNone];
      

      案例 3:一次重新加载多个部分 appledoc

      tableView reloadSections:yourIndexPathSet withRowAnimation:UITableViewRowAnimationNone];
      

      注意 :: 必须在您的 ma​​inThread / gui 线程中执行任何这些操作。

      【讨论】:

      • 问题是[tableview reloadData] 不会刷新自定义单元格中生成的数组。我不知道为什么。其他两个可以解决问题,但问题是每个自定义单元格都有一个带有一组图像的 collectionView,这些图像在第二次刷新后会失去顺序并从一个单元格跳到另一个单元格。
      • 您必须手动更新数组(数据),[tableView reloadData] 并不是要重新加载数据部分,它只是根据您提供的数据重新加载tableView(单元格)。填充新数据后,只需调用[tableView reloadData]。请记住从 mainThread 调用它。希望我明白你的意思。
      • 嗯,这就是我要弄清楚的是如何定位位于特定自定义单元格(我知道 indexPath)中的特定数组,更新它然后调用[tableview reloadData]
      • 我正在做的情况 2 在视图中会出现,因为我想刷新单个表视图单元格但行数据没有改变...如何从主线程调用方法?
      • [[NSOperationQueue mainQueue] addOperationWithBlock:^{ // your code goes here }];
      【解决方案4】:

      解决方案。

      首先,我们需要确保每个自定义单元格都有一个@property NSInteger cellIndex;。当 tableView 填充自定义单元格时,请确保添加cell.cellIndex = indexPath.row;。这样每个自定义单元格都将等于 tableView 行。

      其次,在父viewController中进行更改时,在更新结束时我们会发布通知[[NSNotificationCenter defaultCenter] postNotificationName:@"refreshCollectionView" object:self];

      第三,在自定义单元中,我们收听该通知: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshCollect) name:@"refreshCollectionView" object:nil];。参考 cellIndex 的触发方法(也包含在自定义单元格中):

      -(void) refreshCollect {
      
          if (cellIndex == [[[NSUserDefaults standardUserDefaults] valueForKey:@"cellIndex"] intValue]) {
      
             NSURL *newsImgURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.website.com/core/functions/ios/page.news.image?news_id=%@", [[NSUserDefaults standardUserDefaults] valueForKey:@"newsID"]]];
             NSData *newsImgData = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:newsImgURL] returningResponse:nil error:nil];
      
             newsImgDict    = [NSJSONSerialization JSONObjectWithData:newsImgData options: NSJSONReadingMutableContainers error:nil];
             newsImgID2     = [NSMutableArray arrayWithArray: [newsImgDict valueForKey:@"image_id"]];
             newsImgTime    = [NSMutableArray arrayWithArray: [newsImgDict valueForKey:@"timestamp"]];
             newsIDImg      = [NSMutableArray arrayWithArray: [newsImgDict valueForKey:@"news_id"]];
             newsImgDescr   = [NSMutableArray arrayWithArray: [newsImgDict valueForKey:@"description"]];
             newsImgExt2    = [NSMutableArray arrayWithArray: [newsImgDict valueForKey:@"ext"]];
      
      } } 
      

      在我的例子中,我有 NSDictionary,它只为那个特定的自定义单元重建一些数组。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-11
        • 1970-01-01
        • 1970-01-01
        • 2012-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多