简体   繁体   English

拖放时出现错误“尝试将第 4 行插入第 1 节,但更新后第 1 节中只有 4 行”

[英]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.

相关问题 “尝试将第0行插入第0部分,但更新后第0部分只有0行”错误 - “Attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update” Error “尝试将第0行插入第0部分,但更新后第0部分只有0行” - 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update' 尝试将第0行插入第0部分,但更新后第0部分只有0行 - attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update 尝试将第 3 行插入第 0 节,但更新后第 0 节中只有 3 行 - attempt to insert row 3 into section 0, but there are only 3 rows in section 0 after the update Swift-“尝试将第0行插入第0节,但更新后第0节只有0行” - Swift - “attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update” SwiftUI 应用程序因“尝试将第 2 行插入第 0 部分,但更新后第 0 部分中只有 2 行”错误而崩溃 - SwiftUI app crash with “attempt to insert row 2 into section 0, but there are only 2 rows in section 0 after the update” error 尝试插入行时“尝试将第 0 行插入第 0 节,但更新后第 0 节中只有 0 行” - "attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update" when trying to insert row 在表中插入一行时,“试图将第0行插入第0部分,但更新后第0部分只有0行” - 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update' when inserting a row into a table 'NSInternalInconsistencyException',原因:“试图将第0行插入第0节,但更新后第0节只有0行” - 'NSInternalInconsistencyException', reason: 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update' 'NSInternalInconsistencyException',原因:“试图将第4行插入第0节,但更新后第0节只有4行” - 'NSInternalInconsistencyException', reason: 'attempt to insert row 4 into section 0, but there are only 4 rows in section 0 after the update'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM