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
}