ios 重新加载 UITableView 中的部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32352070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:29:09  来源:igfitidea点击:

Reload sections in UITableView

iosobjective-cuitableview

提问by Reckoner

I am writing an app that notifies user when its time to take his/her medicine. The label at the top of the page shows a date and the tableViewgets populated with medicine names and times that need to be taken on that particular day. Now the sections get populated on the basis of number of medicines to be taken on that day. So the number of sections are dynamically changing over time; a medicine is scheduled to be taken on that particular day. Whenever the user takes a medicine, it is stored in DB as a variable that changes to "1"else it remains "0".

我正在编写一个应用程序,通知用户何时该吃药了。页面顶部的标签显示了日期,并tableView填充了该特定日期需要服用的药物名称和时间。现在,根据当天要服用的药物数量来填充这些部分。所以部分的数量会随着时间动态变化;计划在该特定日期服用药物。每当用户服用药物时,它都会作为一个变量存储在 DB 中,该变量会更改为"1"else 它仍然存在"0"

Also I know that I need to reload section of that particular medicine on that day.

我也知道那天我需要重新加载该特定药物的部分。

Now the question is how do I achieve that? How to reload a particular section?

现在的问题是我如何实现这一目标?如何重新加载特定部分?

for(NSManagedObject *mo in secondEntityArray) {
    NSDate *temp1 = [mo valueForKey:@"date"];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString * temp2 = [dateFormatter stringFromDate:temp1];
    NSDate *temp3 = [dateFormatter dateFromString:temp2];
    NSDate * temp4 = [dateFormatter dateFromString:_dateLabel.text];
    if([temp3 compare:temp4] == NSOrderedSame) {
        if([[mo valueForKey:@"isNotificationFired"] isEqualToString:@"1"] && [[mo valueForKey:@"repeatDays"] isEqualToString:@"Everyday"]) {
            //Reflect changes in tableView section
        }
    }
}

采纳答案by NeverHopeless

There are few functions supported in UITableView for reloading data. See section Reloading the Table Viewof doc.

UITableView 中支持的重新加载数据的功能很少。请参阅重新加载文档的表格视图部分。

  • reloadData
  • reloadRowsAtIndexPaths:withRowAnimation:
  • reloadSections:withRowAnimation:
  • reloadSectionIndexTitles
  • 重新加载数据
  • reloadRowsAtIndexPaths:withRowAnimation:
  • reloadSections:withRowAnimation:
  • reloadSectionIndexTitles

Also, see this similar thread, a part from it:

另外,请参阅这个类似的线程,其中的一部分:

NSRange range = NSMakeRange(0, 1);
NSIndexSet *section = [NSIndexSet indexSetWithIndexesInRange:range];                                     
[self.tableView reloadSections:section withRowAnimation:UITableViewRowAnimationNone];

Also, it is not necessary to reload specific sections, you can just adjust your dataset and call [tableView reloadData], it will load the visible cells only. A part from doc:

此外,没有必要重新加载特定部分,您只需调整数据集并调用[tableView reloadData],它只会加载可见单元格。来自文档的一部分:

Call this method to reload all the data that is used to construct the table, including cells, section headers and footers, index arrays, and so on. For efficiency, the table view redisplays only those rows that are visible. It adjusts offsets if the table shrinks as a result of the reload.

调用此方法以重新加载用于构造表的所有数据,包括单元格、节页眉和页脚、索引数组等。为了效率,表格视图只重新显示那些可见的行。如果表因重新加载而缩小,它会调整偏移量。

回答by Joe Susnick

Swift 3

斯威夫特 3

NSIndexSet is nothing special

NSIndexSet 没什么特别的

tableView.reloadSections([1, 2, 3], with: .none)

It's just an array. Crazy.

它只是一个数组。疯狂的。

回答by Frankie

Update for quick usage in Swift (as of 2.2)

更新以在 Swift 中快速使用(从 2.2 开始)

// index 0 being the section number 1 that you will reload
tableView.reloadSections(NSIndexSet(index: 0), withRowAnimation: .None)

回答by Abhishek Mishra

You can use this also--

你也可以用这个——

indexPath.section- `UITableView section which you want to reload .

indexPath.section- 你要重新加载的`UITableView 部分。

 NSIndexSet *section = [NSIndexSet indexSetWithIndex:indexPath.section];
[self.tableView reloadSections:section withRowAnimation:UITableViewRowAnimationNone];

回答by Aks

Here is the method, you can pass section details in different ways

这是方法,您可以通过不同方式传递部分详细信息

[self.tableView reloadSections:[[NSIndexSet alloc] initWithIndex:1] withRowAnimation:NO];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];

Reloading particular sections improves performance for the table view as well some time it also avoid some issues like floating/moving custom headers-footers in your view. SO try to use reloadSection than relaodData whenever possible

重新加载特定部分可以提高表格视图的性能,有时还可以避免一些问题,例如在视图中浮动/移动自定义页眉页脚。所以尽可能使用 reloadSection 而不是 relaodData

回答by Rajat

Use this method to reload your section

使用此方法重新加载您的部分

- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation