簡體   English   中英

重新加載 tableView 部分而不重新加載標題部分 - Swift

[英]Reload tableView section without reloading header section - Swift

我正在嘗試重新加載 tableView 部分而不是重新加載整個 tableview,因為我在標題部分有一個文本字段,當我調用self.tableView.reloadData()我的鍵盤會關閉。

我已經嘗試了下面的代碼,但Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete row 5 from section 2 which only contains 4 rows before the update'我收到此錯誤Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete row 5 from section 2 which only contains 4 rows before the update'那么請問我的問題在哪里?

    let newCount = self.placeArray.count

    var newIndexPaths = NSMutableArray(capacity: newCount)

    for var i = 0 ; i < newCount ; i++ {
    var indexPath = NSIndexPath(forRow: i, inSection: 2)
        newIndexPaths.addObject(indexPath)
    }

    self.tableView.beginUpdates()
    self.tableView.reloadRowsAtIndexPaths(newIndexPaths as [AnyObject], withRowAnimation: UITableViewRowAnimation.None)
    self.tableView.endUpdates()

您可以改用UITableViewreloadSections方法。

tableView.reloadSections(IndexSet(integer: 2), with: .none)

斯威夫特 3.0.1:

let sectionIndex = IndexSet(integer: 0)

tableView.reloadSections(sectionIndex, with: .none) // or fade, right, left, top, bottom, none, middle, automatic

Swift 3.1 Xcode 8.3.3 答案:)

問題是 - 當然,每個部分都包含標題,所以部分是:

部分 = 頁眉 + 頁腳 + 行

答案是 - 直接通過 IndexPath 重新加載行。 例子:

  1. 假設您的表中有1 個部分和用作表數據源的“數組”:

    let indexes = (0..<array.count).map { IndexPath(row: $0, section: 0) }

  2. 現在我們有了要重新加載的單元格索引數組,所以:

    tableView.reloadRows(at: indexes, with: .fade)

筆記:

  • UITableView.reloadData()將重新加載所有表格。 關於 reloaddata 的文檔

    重新加載用於構建表格的所有數據,包括單元格、節頁眉和頁腳、索引數組等。 為了效率,表視圖只重新顯示那些可見的行。 如果表因重新加載而縮小,它會調整偏移量。 當表視圖的委托或數據源希望表視圖完全重新加載其數據時,它會調用此方法。

  • UITableView.reloadSections(_ sections: IndexSet, with animation: UITableViewRowAnimation)將重新加載特定部分(包括部分頁眉和部分頁腳)。 關於重新加載部分的文檔

  • UITableView.reloadRows(at indexPaths: [IndexPath], with animation: UITableViewRowAnimation)將重新加載指定的行。 關於 reloadRows 的文檔

所以這里reloadSections在這里reloadSections 如果您的標題部分數據和狀態沒有改變,調用此方法將沒有區別。

解決方法:使用reloadRows加載所有節或特定節的行。 注意:在這里你可以只加載可見行,當你滾動查看時會加載其他行。 更多解決方案也可以參考Get all indexPaths in a section

var indexPaths = self.tableView.indexPathsForVisibleRows

/* just roload visible rows of specified section
indexPaths = indexPaths.filter({ (indexPath) -> Bool in
    return indexPath.section == SECTION_INDEX_NEED_TO_RELOAD
})
*/
self.tableView.reloadRows(at: indexPaths!, with: .none)

斯威夫特 5

您可以重新加載部分,如下所示:

tableView.reloadSections([0], with: .none)

或者

tableView.reloadSections(IndexSet(integer: 0), with: .none)

這里代表它將重新加載的索引。

重新加載多個部分:

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

斯威夫特 5

這是放置靜態索引重新加載,它是第一個索引,您可以根據需要進行更改。

self.tblView.reloadSections(IndexSet(index: 1), with: .none)

[斯威夫特 5]

您想重新加載一個部分中的所有行。 所以你可以這樣做:

let rowIndexes: [IndexPath] = Array(0...YOUR_ARRAY-1).map({ 
    return IndexPath(row: $0, section: YOUR_SECTION) 
})

獲取此部分的所有可能的行索引。 然后你只需要為這些索引重新加載行:

tableView.reloadRows(at: rowsIndexes, with: .fade)

該部分不會重新加載。

我可能會遲到,但這里有一個 2D 數組的解決方案(如果您的 tableView 有部分並且每個部分都有一些項目)。 這將重新加載每個部分中的每個單元格,但不會觸及該部分的頁眉/頁腳。

    let indexPaths = (0..<array.count).map { section in
        (0..<array[section].nestedArray.count).map { IndexPath(row: $0, section: section) }
    }.flatMap { $0 }
    tableView.reloadRows(at: indexPaths, with: .fade)

暫無
暫無

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

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