簡體   English   中英

UITableView 重載部分

[英]UITableView reload section

我只想重新加載一個部分而不是整個表格。 UITableView是否有任何方法。

[tableView reloadData]用於加載全表。
我想知道如何僅加載一個部分,因為表中有大量行。

reloadSections 方法給我帶來了麻煩——因為我必須構造一些對象。 如果您需要靈活性,這很好,但有時我也只想要簡單。 它是這樣的:

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

這將重新加載第一部分。 我更喜歡在 UITableView 上有一個類別,然后調用這個方法:

[self.tableView reloadSectionDU:0 withRowAnimation:UITableViewRowAnimationNone];

我的類別方法如下所示:

@implementation UITableView (DUExtensions)

- (void) reloadSectionDU:(NSInteger)section withRowAnimation:(UITableViewRowAnimation)rowAnimation {
    NSRange range = NSMakeRange(section, 1);
    NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range];                                     
    [self reloadSections:sectionToReload withRowAnimation:rowAnimation];
}

就在這里:

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

但是您只能重新加載包含相同行數的部分(或者您必須手動添加或刪除它們) 否則你會得到:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 2. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

使用[tableView reloadData]時不需要。

當您需要重新加載一個部分並且您更改了其中的行數時,您可以使用以下內容:

NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:section];

[self beginUpdates];
    [self deleteSections:indexSet withRowAnimation:rowAnimation];
    [self insertSections:indexSet withRowAnimation:rowAnimation];
[self endUpdates];

如果你把它放在一個類別中(比如 bandejapaisa 節目),它可能看起來像這樣:

- (void)reloadSection:(NSInteger)section withRowAnimation:(UITableViewRowAnimation)rowAnimation {
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:section];

    [self beginUpdates];
        [self deleteSections:indexSet withRowAnimation:rowAnimation];
        [self insertSections:indexSet withRowAnimation:rowAnimation];
    [self endUpdates];
}

對於 Swift 3、4 和 5

let sectionToReload = 1
let indexSet: IndexSet = [sectionToReload]

self.tableView.reloadSections(indexSet, with: .automatic)

正確的方法:

[self.tableView beginUpdates]; 
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];

根據此處接受的答案,我創建了一個函數,該函數將使用動畫重新加載表中的所有部分。 這可能可以通過僅重新加載可見部分來優化。

[self.tableView reloadData];
NSRange range = NSMakeRange(0, [self numberOfSectionsInTableView:self.tableView]);
NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range];
[self.tableView reloadSections:sections withRowAnimation:UITableViewRowAnimationFade];

就我而言,我不得不在部分動畫之前強制重新加載數據,因為表的基礎數據已更改。 但是,它可以正確設置動畫。

嘗試使用

[self.tableView beginUpdates]; 
[self.tableView endUpdates];

希望這能解決您的問題。

這是方法,您可以通過不同方式傳遞部分詳細信息

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

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

重新加載特定部分可以提高表格視圖的性能,有時還可以避免一些問題,例如在視圖中浮動/移動自定義頁眉頁腳。 所以盡可能使用 reloadSection 而不是 relaodData

你需要這個...用於重新加載行

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

或重新加載部分

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

如果您有自定義剖面視圖,您可以在視圖控制器中添加對它的弱引用,並隨時更新它。 這是我的代碼供參考:

@property (weak, nonatomic) UILabel *tableHeaderLabel;

....

-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UITableViewHeaderFooterView *myHeader = [[UITableViewHeaderFooterView alloc] init];

    UILabel *titleLabel = [[UILabel alloc] init];
    [titleLabel setFrame:CGRectMake(20, 0, 280, 20)];
    [titleLabel setTextAlignment:NSTextAlignmentRight];
    [titleLabel setBackgroundColor:[UIColor clearColor]];
    [titleLabel setFont:[UIFont systemFontOfSize:12]];

    [myHeader addSubview:titleLabel];

    self.tableHeaderLabel = titleLabel; //save reference so we can update the header later

    return myHeader;
}

然后你可以像這樣更新你的部分:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.tableHeaderLabel.text = [NSString stringWithFormat:@"Showing row: %ld", indexPath.row];
}

但是您只能重新加載包含相同行數的部分(或者您必須手動添加或刪除它們)。 否則你會得到一個 NSInternalInconsistencyException。

腳步:

  1. 計算要刪除和/或插入的行
  2. 從這些生成一個 IndexPath 數組
  3. 調用相關的tableView方法

現在您可以安全地調用 reloadSections :) 重新加載部分將調用其余索引的更新。

或者您可以使用類似的庫: https : //github.com/onmyway133/DeepDiff

Swift偽代碼:


        tableView.deleteRows(at: valueIndexesToRemove, with: .automatic)
        tableView.insertRows(at: valueIndexesToInsert, with: .automatic)
        tableView.reloadSections(IndexSet([section]), with: .automatic)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM