web-dev-qa-db-fra.com

Actualiser certaines lignes de UITableView en fonction de Int dans Swift

Je suis un développeur débutant dans Swift et je crée une application de base qui comprend un UITableView. Je veux actualiser une certaine ligne de la table en utilisant:

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

et je veux que la ligne qui sera rafraîchie provienne d'un Int nommé rowNumber

Le problème est que je ne sais pas comment faire cela et que toutes les discussions que j'ai recherchées concernent Obj-C.

Des idées?

65
BobRon

Vous pouvez créer une NSIndexPath en utilisant le numéro de ligne et de section, puis le recharger comme suit:

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

Dans cet exemple, j'ai supposé que votre table ne contenait qu'une section (c'est-à-dire 0) mais vous pouvez modifier cette valeur en conséquence.

Mise à jour pour Swift 3.0:

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

Pour une solution d'animation à impact modéré:

Swift 3:

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

Swift 2.x:

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

C’est un autre moyen de protéger l’application contre le crash:

Swift 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:

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

Dans 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)

par défaut, si vous n’avez qu’une section dans TableView, vous pouvez indiquer 0.

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

Swift 4

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

De plus, si vous avez sections pour tableview, vous ne devez pas essayer de rechercher toutes les lignes que vous souhaitez actualiser, vous devez utiliser des sections de rechargement. C'est un processus facile et plus équilibré:

yourTableView.reloadSections(IndexSet, with: UITableViewRowAnimation)
0
Burcu K. Kutluay

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
Legend_ 33

Je me rends compte que cette question est pour Swift, mais voici le code équivalent Xamarin de la réponse acceptée si quelqu'un est intéressé.

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

Swift 4.1

utilisez-le lorsque vous supprimez une ligne à l'aide de selectedTag of row. 

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)
0
Rana Ali Waseem

Que diriez-vous:

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