在刷新斯威夫特基于诚信的UITableView的某一行

问题描述 投票:69回答:10

我一开始在斯威夫特开发人员,我创建一个基本的应用程序,包括一个UITableView。我想刷新使用该表的某一行:

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

我想,这将刷新是从一个Int命名ROWNUMBER行

问题是,我不知道如何做到这一点,所有我搜索的线程的对象 -

有任何想法吗?

ios uitableview swift nsindexpath
10个回答
172
投票

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

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

在这个例子中,我假定你的表只有一个部分(即0),但你可能会相应地改变该值。

更新雨燕3.0:

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

0
投票

我意识到这个问题是斯威夫特,但这里是公认的答案的Xamarin等效代码,如果有人有兴趣。

var indexPath = NSIndexPath.FromRowSection(rowIndex, 0);
tableView.ReloadRows(new NSIndexPath[] { indexPath }, UITableViewRowAnimation.Top);

20
投票

对于软影响动画的解决方案:

斯威夫特3:

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

斯威夫特2.X:

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

这是保护应用程序崩溃另一种方式:

斯威夫特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)
    }
}

斯威夫特2.X:

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

3
投票

斯威夫特4

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

2
投票

在雨燕3.0

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

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

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

默认情况下,如果你只有一个TableView中部分,然后就可以把区间值0。


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

1
投票

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

0
投票

怎么样:

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

0
投票

此外,如果您有实现代码如下节,你不应该试图找到你想要刷新,你应该使用重载的部分每行。这是很容易和更平衡的过程:

yourTableView.reloadSections(IndexSet, with: UITableViewRowAnimation)

0
投票

雨燕4.1

使用它,当你删除使用行的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)
© www.soinside.com 2019 - 2024. All rights reserved.