Home > OS >  Invalid update: invalid number of rows in section 0 error occur
Invalid update: invalid number of rows in section 0 error occur

Time:12-07

This is my struct

struct CommunityListRM: Codable {
    let status       : Bool?
    let communityList: [CommunityList]?
    
    struct CommunityList: Codable {
        var id              : String?
        var user_id         : String?
        var icon            : String?
        var nickname        : String?
        var profile_image   : String?
        var content         : String?
        var image_1_url     : String?
        var image_2_url     : String?
        var image_3_url     : String?
        var image_4_url     : String?
        var image_5_url     : String?
        var image_ratio1    : String?
        var image_ratio2    : String?
        var image_ratio3    : String?
        var image_ratio4    : String?
        var image_ratio5    : String?
        var comment_qty     : String?
        var like_qty        : String?
        var date            : String?
        var created_at      : String?
        var updated_at      : String?
        var like_it         : String?
    }
}

I want to insertRows like instagram feeds

var posts        : [CommunityListRM.CommunityList] = []

NetworkService().request(.communityList, params, header) { [self] (response: DataResponse<CommunityListRM,AFError>) in
    switch response.result {
        case .success(let data):
        if let nComList = data.communityList {

           self.posts.append(contentsOf: nComList)
           print("selfpostcount\(self.posts.count)")
           self.tableview.beginUpdates()
           self.tableview.insertRows(at: [IndexPath(row: self.posts.count - 1, section: 0)], with: .none)
           self.tableview.endUpdates()
       }
    }
}

If i tried this code error occur

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (10) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out). Table view: <UITableView: 0x104125c00; frame = (0 0; 414 721); clipsToBounds = YES; autoresize = RM BM; gestureRecognizers = <NSArray: 0x281a62b20>; layer = <CALayer: 0x2814a0b40>; contentOffset: {0, 0}; contentSize: {414, 0}; adjustedContentInset: {0, 0, 0, 0};

Help me plz

CodePudding user response:

First of all begin-/endUpdates() is pointless for a single insert/delete/move operation.


The error occurs because you are adding multiple items to the array but insert only one row. You have to specify all index paths.

  • If posts is empty map all indices of posts to index paths

    self.posts.append(contentsOf: nComList)
    let indexPaths = self.posts.indices.map{IndexPath(row: $0, section: 0)}
    self.tableview.insertRows(at: indexPaths, with: .automatic)
    

    or – as you don't specify any animation – just reload the table view

    self.posts.append(contentsOf: nComList)
    self.tableview.reloadData()
    
  • If posts is not empty count the items in the array before and after appending the items to get start and end index

    let startIndex = self.posts.count
    self.posts.append(contentsOf: newArray)
    let endIndex = self.posts.count
    let indexPaths = (startIndex..<endIndex).map{IndexPath(row: $0, section: 0)}
    self.tableview.insertRows(at: indexPaths, with: .automatic)
    
  • Related