重新加载UITableView中的部分 [英] Reload sections in UITableView

查看:80
本文介绍了重新加载UITableView中的部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个通知用户何时需要服药的应用程序.页面顶部的标签显示日期,而tableView填充有该特定日期需要服用的药物名称和时间.现在,根据当天要服用的药物数量填充这些部分.因此,部分的数量会随着时间动态变化.预定在该特定日期服用某种药物.每当用户服用药物时,该药物都会作为变量存储在DB中,该变量会更改为"1",否则它将保留为"0".

我也知道那天我需要重新装填特定药物的一部分.

现在的问题是我该如何实现?如何重新加载特定部分?

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
        }
    }
}

解决方案

UITableView中支持的用于重新加载数据的功能很少.参见重新加载文档的表视图. >

  • reloadData
  • reloadRowsAtIndexPaths:withRowAnimation:
  • reloadSections:withRowAnimation:
  • reloadSectionIndexTitles

另外,请参见此相似的线程,它是其中的一部分:

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

此外,不必重新加载特定部分,您只需调整数据集并调用[tableView reloadData],它将仅加载可见的单元格. doc的一部分:

调用此方法以重新加载用于构造 表,包括单元格,节的页眉和页脚,索引数组,以及 很快.为了提高效率,表格视图仅重新显示那些 是可见的.如果表由于以下原因而缩小,则会调整偏移量 重新加载.

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 tableView gets 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".

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
        }
    }
}

解决方案

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

  • reloadData
  • 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:

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.

这篇关于重新加载UITableView中的部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆