简体   繁体   English

在TableView自定义单元格中刷新数组

[英]Refresh array in TableView Custom Cell

I have a ViewController that has a TableView generated with Custom Cells. 我有一个ViewController,其中有一个使用自定义单元格生成的TableView。 Each Custom Cell has a NSDictionary that generates few arrays. 每个自定义单元都有一个NSDictionary,该NSDictionary生成几个数组。

I want to find the way to replace one array in Custom Cell using [myArray replaceObjectAtIndex:... withObject:...] pointing towards a specific cell in a TableView , provided that I already know the indexPath of that cell that contains that array. 我想找到一种方法来使用[myArray replaceObjectAtIndex:... withObject:...] 指向TableView中的特定单元格来替换自定义单元格中的一个数组,前提是我已经知道包含该数组的该单元格的indexPath

Another words I have to somehow indicate that Custom Cell indexPath, get there and refresh array. 我必须以某种方式表示自定义单元格indexPath的其他单词,到达该位置并刷新数组。

Is there a way to accomplish that mission in Objective-C? 有没有办法在Objective-C中完成该任务?

You can get a cell reference using cellForRowAtIndexPath call if you know the indexPath of the cell that you want to change if each of your custom cell is different unique. 如果您知道每个自定义单元格都不同时要更改的单元格的indexPath,则可以使用cellForRowAtIndexPath调用获取单元格引用。

But I strongly recommend changing the data model by getting it using cell location as the reference. 但我强烈建议您通过使用单元格位置作为参考来获取数据模型来更改数据模型。 Because table views reuse cell you could end up with unexpected results. 由于表视图会重用单元格,因此您可能会得到意想不到的结果。 You should instead change your table model and then reload the table or perhaps reload just the specific cells that have changed. 相反,您应该更改表模型,然后重新加载表,或者也许仅重新加载已更改的特定单元格。

Once you have the indexPath of your cell, you can do something like: 有了单元格的indexPath后,您可以执行以下操作:

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

You got several option for reloading the UITableView taken inside the UIViewController 您有几个选项可以重新加载在UIViewController内部获取的UITableView

say your table view is such 说你的表视图是这样的

    UITableView *tableView

case 1: Reload the whole table view appledoc 情况1: 重新加载整个表视图 appledoc

[tableView reloadData];

case 2: Reload all the rows inside a section appledoc 情况2: 重新加载 appledoc 节中的所有行

[tableView reloadRowsAtIndexPaths:yourIndexPath withRowAnimation: UITableViewRowAnimationNone];

case 3: Reload multiple section at once appledoc 情况3: 一次重新加载多个部分 appledoc

tableView reloadSections:yourIndexPathSet withRowAnimation:UITableViewRowAnimationNone];

NB :: must do any of these operation in your mainThread / gui thread . 注意:::必须在mainThread / gui线程中执行任何这些操作。

Solution. 解。

First, we need to make sure that every Custom Cell has a @property NSInteger cellIndex; 首先,我们需要确保每个自定义单元格都有一个@property NSInteger cellIndex; . When tableView populates Custom Cells, make sure to add cell.cellIndex = indexPath.row; 当tableView填充“自定义单元格”时,请确保添加cell.cellIndex = indexPath.row; . That way every Custom Cell will have it equal to tableView row. 这样,每个自定义单元格将使其等于tableView行。

Second, when making changes in parent viewController, in the end of update we post notification [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshCollectionView" object:self]; 第二,在父viewController中进行更改时,在更新结束时,我们会发布通知[[NSNotificationCenter defaultCenter] postNotificationName:@"refreshCollectionView" object:self]; .

Third, in Custom Cell we listen to that notification: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshCollect) name:@"refreshCollectionView" object:nil]; 第三,在“定制单元”中,我们收听该通知: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshCollect) name:@"refreshCollectionView" object:nil]; . That triggers method (also included in Custom Cell) with reference to cellIndex: 引用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"]];

} } 

In my case I have NSDictionary that rebuilds some arrays just for that particular Custom Cell. 就我而言,我有NSDictionary可以为特定的自定义单元格重建一些数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM