'Getting an error "attempt to insert row 4 into section 1, but there are only 4 rows in section 1 after the update" while drop and drag
I'm trying to implement drop and drag, but getting an error: attempt to insert row 4 into section 1, but there are only 4 rows in section 1 after the update
I get this error, when try to drop item from Reminder to my tableView.
I have this code im my ViewController:
extension ProfileViewController: UITableViewDropDelegate {
func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) {
let destinationIndexPath = IndexPath(row: tableView.numberOfRows(inSection: 1), section: 1)
let item = coordinator.items[0]
switch coordinator.proposal.operation {
case .copy:
print("Copying...")
let itemProvider = item.dragItem.itemProvider
itemProvider.loadObject(ofClass: NSString.self) { string, error in
if (string as? String) != nil {
let fasting = Fasting(autor: "Author", description: "Description", image: UIImage(named: "регби") ?? UIImage(), numberOfLikes: 0, numberOfviews: 0)
self.addGeocache(fasting, at: destinationIndexPath.row)
DispatchQueue.main.async {
tableView.insertRows(at: [destinationIndexPath], with: .automatic)
}
}
}
default:
return
}
}
Here I create numberOfRows im my tableView:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return 1
} else {
return Flow.sections.fasting.count
}
}
This is my array of elements in section 1:
struct Flow {
static let sections = FastingSections(
fasting: [
Fasting(autor: "Gustav",
description: "Today i did eat this",
image: UIImage(named: "эко-мороженое 1")!,
numberOfLikes: 100,
numberOfviews: 100),
Fasting(autor: "Dasha",
description: "I love this watch",
image: UIImage(named: "часы")!,
numberOfLikes: 200,
numberOfviews: 200),
Fasting(autor: "Misha",
description: "Playing rugby with my friends",
image: UIImage(named: "регби")!,
numberOfLikes: 300,
numberOfviews: 300),
Fasting(autor: "Nikita",
description: "Soon...",
image: UIImage(named: "cosmos")!,
numberOfLikes: 400,
numberOfviews: 400),
]
)
}
Solution 1:[1]
Here
let fasting = Fasting(autor: "Author", description: "Description", image: UIImage(named: "?????") ?? UIImage(), numberOfLikes: 0, numberOfviews: 0)
self.addGeocache(fasting, at: destinationIndexPath.row)
It seems you add a new object to other data source other than Flow.sections.fastinghence the error while you need to change
static let sections = FastingSections(....
to
static var sections = FastingSections(....
and make any addition/deletion to it
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sh_Khan |
