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),
]
)
}