Jessica
Jessica

Reputation: 9830

Animate UITableViewCell gives me error

I have a UITableView. I want to add a cell to the beginning of the tableView with animation. I tried the following:

let indexPath = NSIndexPath(forRow: 0, inSection: 0)
tableView.reloadRowsAtIndexPaths(indexPath, withRowAnimation: UITableViewRowAnimation.Automatic)

But I get the following error:

Cannot invoke 'reloadRowsAtIndexPaths' with an argument list of type '(NSIndexPath!, withRowAnimation: UITableViewRowAnimation)'

(When I just do tableView.reloadData() it works fine.)

What am I doing wrong, and how can I fix it?

Upvotes: 1

Views: 64

Answers (2)

Syren
Syren

Reputation: 31

You get the error, because reloadRowsAtIndexPaths expects an array of index paths, not a single index path. However reloadRowsAtIndexPaths wouldn't add a cell, but just reload the cells at the specified index paths.

Instead you should use insertRowsAtIndexPaths to add your cell.

Upvotes: 0

der_michael
der_michael

Reputation: 3381

tableView.reloadRowsAtIndexPaths expects an [NSIndexPath] instead of an NSIndexPath! as first argument.

so fix it by calling

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

Upvotes: 1

Related Questions