简体   繁体   English

重新加载 tableView 部分而不重新加载标题部分 - Swift

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

I'm trying to reload tableView section instead of reloading the whole tableview because I have a textfield in the header section and when I call self.tableView.reloadData() my keyboard get closed.我正在尝试重新加载 tableView 部分而不是重新加载整个 tableview,因为我在标题部分有一个文本字段,当我调用self.tableView.reloadData()我的键盘会关闭。

I've tried the code below but I get this error Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete row 5 from section 2 which only contains 4 rows before the update' So please where would be my issue?我已经尝试了下面的代码,但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)

Swift 3.0.1:斯威夫特 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 Answer :) Swift 3.1 Xcode 8.3.3 答案:)

The problem is - each section includes header of course, so section is:问题是 - 当然,每个部分都包含标题,所以部分是:

section = header + footer + rows部分 = 页眉 + 页脚 + 行

The answer is - reload rows directly by IndexPath.答案是 - 直接通过 IndexPath 重新加载行。 Example:例子:

  1. Let's say you've got 1 section in your table and 'array' of something you use as datasource for table:假设您的表中有1 个部分和用作表数据源的“数组”:

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

  2. Now we've got array of indexes of cell we want to reload, so:现在我们有了要重新加载的单元格索引数组,所以:

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

Note:笔记:

  • UITableView.reloadData() will reload all the table. UITableView.reloadData()将重新加载所有表格。 Docs about reloaddata 关于 reloaddata 的文档

    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.如果表因重新加载而缩小,它会调整偏移量。 The table view's delegate or data source calls this method when it wants the table view to completely reload its data.当表视图的委托或数据源希望表视图完全重新加载其数据时,它会调用此方法。

  • UITableView.reloadSections(_ sections: IndexSet, with animation: UITableViewRowAnimation) will reload specific section( including section header and section footer ). UITableView.reloadSections(_ sections: IndexSet, with animation: UITableViewRowAnimation)将重新加载特定部分(包括部分页眉和部分页脚)。 Docs about reloadsections 关于重新加载部分的文档

  • UITableView.reloadRows(at indexPaths: [IndexPath], with animation: UITableViewRowAnimation) will reload the specified rows. UITableView.reloadRows(at indexPaths: [IndexPath], with animation: UITableViewRowAnimation)将重新加载指定的行。 Docs about reloadRows 关于 reloadRows 的文档

So here reloadSections will not works here.所以这里reloadSections在这里reloadSections If your header section data and status not change, call this method will make no diffrence by sight.如果您的标题部分数据和状态没有改变,调用此方法将没有区别。

Solution: use reloadRows to load the rows of all the sections or of the specific section.解决方法:使用reloadRows加载所有节或特定节的行。 Note: here you can just load the visible rows, others will be loaded when you scroll you view.注意:在这里你可以只加载可见行,当你滚动查看时会加载其他行。 You also can refer to Get all indexPaths in a section for more solutions更多解决方案也可以参考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)

Swift 5斯威夫特 5

You can just reload section as below:您可以重新加载部分,如下所示:

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

or或者

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

Here zero represent the index it will reload.这里代表它将重新加载的索引。

Reload multiple sections:重新加载多个部分:

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

Swift 5斯威夫特 5

Here is put static index reload which is 1st index, you can change as per your requirement.这是放置静态索引重新加载,它是第一个索引,您可以根据需要进行更改。

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

[Swift 5] [斯威夫特 5]

You want to reload all rows in a section.您想重新加载一个部分中的所有行。 So you can do:所以你可以这样做:

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

To get all possible row indexes for this section.获取此部分的所有可能的行索引。 Then you just have to reload rows for these indexes:然后你只需要为这些索引重新加载行:

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

The section won't be reloaded.该部分不会重新加载。

I might be late to the party but here is a solution for 2D array (in case if your tableView has sections and each section has some items).我可能会迟到,但这里有一个 2D 数组的解决方案(如果您的 tableView 有部分并且每个部分都有一些项目)。 This will reload every cell in every section but won't touch header/footer of the section.这将重新加载每个部分中的每个单元格,但不会触及该部分的页眉/页脚。

    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