Silver Challenge: Preventing Reordering

First you need to add one UITableViewCell in your UITableView for de new “No more Items!”

To accomplish this work you need to add one to the count of itemStore without change ItemStore:

 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    //Adding the row for no more elements string
    return itemStore.allItems.count + 1
}

Then you need to put fill the new row with the string “No more Items!” in the delegate method:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.row < itemStore.allItems.count{
        let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell",for: indexPath )
        let item = itemStore.allItems[indexPath.row]
        cell.textLabel?.text = item.name
        cell.detailTextLabel?.text = "$" + String(item.valueInDollars)
        return cell;
    }else{
        let cell = UITableViewCell(style: .value1, reuseIdentifier: "UITableViewCell_NMI")
        cell.textLabel?.text = "No more Items!"
        return cell
    }
}

You can Run the app and see the new row at the bottom of your table, but we don’t have finished yet.

We need to prevent edition of the last row, so, we will add a delegate method of dataSource, in this method we have been implemented that just the index of the itemStore could be edited.

override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    if (indexPath.row >= itemStore.allItems.count){
        return false
    }
    return true
}

And the challenge is now finished.

Note: If you try to move the rows under the last row, the app will crash, in the Gold Challenge we will get solve both.