0

I have a tableView with Header section, And every section have some cells. And each cell have a Reply button to increase tableView Cell height. So Initially I am showing only 3 cells in every section(If cells are more than 3), And on click Show total comment button(In section), It shows all cells. My question is- If I click on Reply button, the cell height is increasing smoothly, But when I click on Show total comment button(In section) and than click Reply button, app is crashing

App Crash Log-

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 2. The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (17), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).

Code Snippet-

@IBOutlet weak var wallFeedTableView: UITableView!
var homeFeedDataList = [HomeWallFeedModelClass]()
var isShowMore = false
var selectedSection = NSNotFound
var shouldCellBeExpanded:Bool = false
var indexOfExpendedCell:NSInteger = -1


// To Expand TableView Cell
@objc func btnReplyCommentAction(button:UIButton) {
    shouldCellBeExpanded = !shouldCellBeExpanded
    indexOfExpendedCell = button.tag
    if shouldCellBeExpanded {
        self.wallFeedTableView.beginUpdates()
        self.wallFeedTableView.endUpdates()
        button.setTitle("Cancel", for: UIControl.State.normal)
    }
    else {
        self.wallFeedTableView.beginUpdates()
        self.wallFeedTableView.endUpdates()
        button.setTitle("Reply", for: UIControl.State.normal)
    }
}
// To expand TableView Section
@objc private func showAllComment(sender: UIButton) {
    let section = sender.tag
    self.isShowMore = true
    self.selectedSection = section
    self.wallFeedTableView.reloadData()
    let indexPath = IndexPath(row: NSNotFound, section: section)
    wallFeedTableView.scrollToRow(at: indexPath, at: .top, animated: false)
    
}

//Cell For Row Method

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let cell = tableView.dequeueReusableCell(withIdentifier: "FeedHomeTableViewCell", for: indexPath) as! FeedHomeTableViewCell
    cell.delegate = self
    let user = AppUser.sharedInstance
    let model = self.homeFeedDataList[indexPath.section].wallList[indexPath.row]
    
    cell.lblComment.attributedText = model.comment
    
    cell.lblComment.sizeToFit()
    cell.lblComment.layoutIfNeeded()
    
    cell.btnReplyComment.tag = indexPath.row
    cell.btnReplyComment.addTarget(self, action: #selector(btnReplyCommentAction), for: .touchUpInside)
    
    return cell
}
//Number Of Sections Method
func numberOfSections(in tableView: UITableView) -> Int {
    return self.homeFeedDataList.count
}
// Number Of Rows In Section Method
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
    if isShowMore == true &&  section == selectedSection{
        self.isShowMore = false
        return self.homeFeedDataList[section].wallList.count
    }else{
        if self.homeFeedDataList[section].wallList.count > 3{
            return 3
        }else{
            return self.homeFeedDataList[section].wallList.count
        }
    }
}

// viewForHeaderInSection Method
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
    let cell = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HomeFeedHeaderView") as! HomeFeedHeaderView
    let model = self.homeFeedDataList[section]
    
    
    cell.backgroundColor = #colorLiteral(red: 0.7450980544, green: 0.1568627506, blue: 0.07450980693, alpha: 1)
    cell.comntTxtField.delegate = self
    cell.lblTitleNews.text = model.title
    
    cell.btnShowCmnt.frame = CGRect(x: 10, y: cell.comntTxtField.frame.origin.y+cell.comntTxtField.frame.size.height+3, width: screenSize.width/2, height: 20)
    
    if self.homeFeedDataList[section].wallList.count > 3 {
        cell.btnShowCmnt.isHidden = false
        cell.contentView.bringSubviewToFront(cell.btnShowCmnt)
        cell.btnShowCmnt.setTitle("Show total comments (\(self.homeFeedDataList[section].wallList.count))", for: .normal)
    }else{
        cell.btnShowCmnt.isHidden = true
    }
    
    if selectedSection == section {
        cell.btnShowCmnt.isHidden = true
    }
    cell.btnShowCmnt.tag = section
    cell.btnShowCmnt.addTarget(self,
                               action: #selector(self.showAllComment(sender:)),
                               for: .touchUpInside)
    return cell
}
iDeveloper
  • 2,339
  • 2
  • 24
  • 38
  • 1
    Please post relevant code only. I can't see anywhere that inserts/deletes table view row. Also you have an empty batch operations call with begin/end updates, that does absolutely nothing. – Desdenova Aug 02 '21 at 07:49
  • @Desdenova There is nothing like inserts or deletes. If I expand my cell after expanding my section, App is crashing. – iDeveloper Aug 02 '21 at 07:51
  • How do you expand/collapse without an insert/delete call. Seems like that's exactly your problem. – Desdenova Aug 02 '21 at 07:52
  • @Desdenova Please check my updated question- I am expanding cell and section by- **// To Expand TableView Cell** And **// To expand TableView Section** – iDeveloper Aug 02 '21 at 08:00
  • Ok. Whatever it is, it's wrong. Let me try to simplify things for you; 1. Update your data source (homeFeedDataList). 2. Call `insertRows(at:with:)` on your table view if you added data to your data source, or `deleteRows(at:with:)` if you removed data. Both methods have a `section` counterparts if you need it. – Desdenova Aug 02 '21 at 08:10
  • Here is how it is done. Although it's in Obj-C, you'll get the idea of it. https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/TableView_iPhone/ManageInsertDeleteRow/ManageInsertDeleteRow.html – Desdenova Aug 02 '21 at 08:14
  • What's the difference between shouldCellBeExpanded and !shouldCellBeExpanded besides the button name? – El Tomato Aug 02 '21 at 12:54
  • @ElTomato I got the solution for Increase and decrease the height of cell on click, By using this solution- https://stackoverflow.com/questions/35515597/uitableview-how-to-change-cell-height-dynamically-when-a-button-is-clicked-in-i – iDeveloper Aug 02 '21 at 13:24

0 Answers0