[英]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我正在尝试实现拖放,但出现错误:尝试将第 4 行插入第 1 节,但更新后第 1 节中只有 4 行
I get this error, when try to drop item from Reminder to my tableView
.尝试将项目从 Reminder 放到我的tableView
时出现此错误。 I have this code im my ViewController:我的 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
:在这里,我在我的tableView
创建了numberOfRows
:
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:这是我在第 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),
]
)
}
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.fasting
hence the error while you need to change似乎您将新的 object 添加到Flow.sections.fasting
以外的其他数据源,因此在您需要更改时出现错误
static let sections = FastingSections(....
to到
static var sections = FastingSections(....
and make any addition/deletion to it并对其进行任何添加/删除
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.