ios Swift 中基于 Int 刷新 UITableView 的某一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28206492/
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 04:35:02  来源:igfitidea点击:

Refresh certain row of UITableView based on Int in Swift

iosuitableviewswiftnsindexpath

提问by SentientBacon

I am a beginning developer in Swift, and I am creating a basic app that includes a UITableView. I want to refresh a certain row of the table using:

我是 Swift 的初级开发人员,我正在创建一个包含 UITableView 的基本应用程序。我想使用以下方法刷新表的某一行:

self.tableView.reloadRowsAtIndexPaths(paths, withRowAnimation: UITableViewRowAnimation.none)

and I want the row that will be refreshed to be from an Int named rowNumber

我希望将刷新的行来自名为 rowNumber 的 Int

The problem is, I don't know how to do this and all the threads I've searched are for Obj-C

问题是,我不知道该怎么做,我搜索的所有线程都是针对 Obj-C

Any ideas?

有任何想法吗?

回答by Lyndsey Scott

You can create an NSIndexPathusing the row and section number then reload it like so:

您可以NSIndexPath使用行号和节号创建一个,然后像这样重新加载它:

let indexPath = NSIndexPath(forRow: rowNumber, inSection: 0)
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top)

In this example, I've assumed that your table only has one section (i.e. 0) but you may change that value accordingly.

在此示例中,我假设您的表只有一个部分(即 0),但您可以相应地更改该值。

Update for Swift 3.0:

Swift 3.0 更新:

let indexPath = IndexPath(item: rowNumber, section: 0)
tableView.reloadRows(at: [indexPath], with: .top)

回答by Alessandro Ornano

For a soft impact animation solution:

对于软冲击动画解决方案:

Swift 3:

斯威夫特 3:

let indexPath = IndexPath(item: row, section: 0)
tableView.reloadRows(at: [indexPath], with: .fade)

Swift 2.x:

斯威夫特 2.x:

let indexPath = NSIndexPath(forRow: row, inSection: 0)
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

This is another way to protect the app from crashing:

这是保护应用程序免于崩溃的另一种方法:

Swift 3:

斯威夫特 3:

let indexPath = IndexPath(item: row, section: 0)
if let visibleIndexPaths = tableView.indexPathsForVisibleRows?.index(of: indexPath as IndexPath) {
    if visibleIndexPaths != NSNotFound {
        tableView.reloadRows(at: [indexPath], with: .fade)
    }
}

Swift 2.x:

斯威夫特 2.x:

let indexPath = NSIndexPath(forRow: row, inSection: 0)
if let visibleIndexPaths = tableView.indexPathsForVisibleRows?.indexOf(indexPath) {
   if visibleIndexPaths != NSNotFound {
      tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
   }
}

回答by Neha

Swift 4

斯威夫特 4

let indexPathRow:Int = 0    
let indexPosition = IndexPath(row: indexPathRow, section: 0)
tableView.reloadRows(at: [indexPosition], with: .none)

回答by Sachin Rasane

let indexPathRow:Int = 0
let indexPosition = IndexPath(row: indexPathRow, section: 0)
tableView.reloadRows(at: [indexPosition], with: .none)

回答by jaiswal Rajan

In Swift 3.0

在 Swift 3.0 中

let rowNumber: Int = 2
let sectionNumber: Int = 0

let indexPath = IndexPath(item: rowNumber, section: sectionNumber)

self.tableView.reloadRows(at: [indexPath], with: .automatic)

byDefault, if you have only one section in TableView, then you can put section value 0.

默认情况下,如果您在 TableView 中只有一个部分,那么您可以将部分值设置为 0。

回答by Legend_ 33

SWIFT 4.2

斯威夫特 4.2

    func reloadYourRows(name: <anyname>) {
    let row = <your array name>.index(of: <name passing in>)
    let reloadPath = IndexPath(row: row!, section: 0)
    tableView.reloadRows(at: [reloadPath], with: .middle)
    }

回答by irwin B

    extension UITableView {
        /// Reloads a table view without losing track of what was selected.
        func reloadDataSavingSelections() {
            let selectedRows = indexPathsForSelectedRows

            reloadData()

            if let selectedRow = selectedRows {
                for indexPath in selectedRow {
                    selectRow(at: indexPath, animated: false, scrollPosition: .none)
                }
            }
        }
    }

tableView.reloadDataSavingSelections()

回答by skyline75489

How about:

怎么样:

self.tableView.reloadRowsAtIndexPaths([NSIndexPath(rowNumber)], withRowAnimation: UITableViewRowAnimation.Top)

回答by Burcu Kutluay

In addition, If you have sectionsfor tableview, you should not try to find every rows you want to refresh, you should use reload sections. It is easy and more balanced process:

此外,如果您有用于 tableview 的部分,则不应尝试查找要刷新的每一行,而应使用重新加载部分。这是一个简单且更平衡的过程:

yourTableView.reloadSections(IndexSet, with: UITableViewRowAnimation)

回答by Rana Ali Waseem

Swift 4.1

斯威夫特 4.1

use it when you delete row using selectedTag of row.

当您使用行的 selectedTag 删除行时使用它。

self.tableView.beginUpdates()

        self.yourArray.remove(at:  self.selectedTag)
        print(self.allGroups)

        let indexPath = NSIndexPath.init(row:  self.selectedTag, section: 0)

        self.tableView.deleteRows(at: [indexPath as IndexPath], with: .automatic)

        self.tableView.endUpdates()

        self.tableView.reloadRows(at: self.tableView.indexPathsForVisibleRows!, with: .automatic)