Error when trying to delete cell to tableView - swift

When I try to delete a cell I get this error:
Thread 1: "Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 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: 0x7fa813016a00; frame = (0 0; 375 667); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x600000e7ca50>; layer = <CALayer: 0x600000083f00>; contentOffset: {0, -116}; contentSize: {375, 2461.5}; adjustedContentInset: {116, 0, 0, 0}; dataSource: <tableRemove.CustomViewController: 0x7fa817d04280>>"
Full implementation of work with tableView looks like this:
extension CustomViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemList[section].itemToList.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
model[section].nameItem
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
let text = model[indexPath.section].itemToList[indexPath.row].nameFood
cell.textLabel?.text = text
return cell
}
func numberOfSections(in tableView: UITableView) -> Int {
return itemList.count
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
true
}
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
model.swapAt(sourceIndexPath.row, destinationIndexPath.row)
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return .delete
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
tableView.beginUpdates()
model[indexPath.section].itemToList.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .automatic)
tableView.endUpdates()
}
}
}

Related

Header view not showing at Section 0

I add custom tableView header. My section count is 2 and the header is not displaying at section 0. But showing properly at section 1.
func numberOfSections(in tableView: UITableView) -> Int {
return arrRouteDetials.count
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
headerRadioBtn.tag = section
headerRadioBtn.addTarget(self, action:#selector(RdAction(_sender: )), for: .touchUpInside)
return viewTblHeader
}else{
headerRadioBtn.tag = section
headerRadioBtn.addTarget(self, action:#selector(RdAction(_sender: )), for: .touchUpInside)
return viewTblHeader
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrOfSectionRowValue[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "BranchUnloadingcell", for: indexPath) as! BranchUnloadingcell
cell.lblSerialNo.text = "\(indexPath.row + 1)"
cell.val = arrOfSectionRowValue[indexPath.section][indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
self.viewWillLayoutSubviews()
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 74
}

Scroll to bottom TableView with custom section and cell Swift

My tableview has custom sections and cells. Every time the user scroll to the bottom it will call the server and load additional datas. My problem is how do I detect the last section so it could load more data? Each section contain 1 to 3 rows. My willDisplay wasn't working properly and the refreshControl.beginRefreshing() indicator didn't show too. Thanks!
let refreshControl = UIRefreshControl()
//MARK - Header Sections
func numberOfSections(in tableView: UITableView) -> Int {
return tableData[segmentedControlIndex].count
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: "headerCell") as! HomeSectionTableViewCell
let model = events[segmentedControlIndex][section]
cell.setup(model: model)
.
.
.
return cell.contentView
}
//MARK - Cells
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData[segmentedControlIndex][section].rows.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "detailCell") as! HomeTableViewDetailCell
.
.
.
cell.detailTitleLabel?.text = tableData[segmentedControlIndex][indexPath.section].rows[indexPath.row].detailTitleLabelString
cell.detailSubtitleLabel?.text = tableData[segmentedControlIndex][indexPath.section].rows[indexPath.row].detailSubtitleLabelString
return cell
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.000001
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell: HomeTableViewDetailCell = tableView.cellForRow(at: indexPath) as? HomeTableViewDetailCell {
cell.detailTitleLabel?.numberOfLines = cell.detailTitleLabel?.numberOfLines == 0 ? 1 : 0
tableView.reloadData()
}
}
//MARK - TableView add more data
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.section == tableView.numberOfSections - 1 {
self.refreshControl.beginRefreshing()
self.page = self.page + 1
self.getMoreData() { result in
self.refreshControl.endRefreshing()
print("Load More Bets Data reload: \(result == true ? "Success":"Failure")")
}
}
}

tableview loading invisible rows as well , cellforrowat index path getting celled for hidden rows

Tableview loading invisible rows as well , cellForRowAtIndexPath getting celled for hidden rows
func numberOfSections(in tableView: UITableView) -> Int {
return ordersArray.count;
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let selectedOrder = ordersArray[indexPath.section]
return CGFloat(180 + (30 * selectedOrder.lineItems().count ));
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if(indexPath.row == 1)
{
let deliveryShipChargesCustomCell =
tableView.dequeueReusableCell(withIdentifier: "DeliveryShipChargesCustomCell", for: indexPath) as! DeliveryShipChargesCustomCell
return deliveryShipChargesCustomCell;
}
else
{
let orderCustomCell = tableView.dequeueReusableCell(withIdentifier: "OrdersCustomCell" , for: indexPath) as! OrdersCustomCell
return orderCustomCell;
}
}
problem with this code snippet is that it gets load row that are not even visible for example if order array count is 10. and there are two rows in each section , initially tableview (_ tableView: UITableView, cellForRowAtIndexPath: IndexPath) gets called for 20 times.
Any thoughts?

Delete UITableHeader title when section contains empty row

I am implementing a UITableView to populate contact name of the persons. I have implemented pretty much every thing.
I need to delete the section header title of the section whose rows have been deleted by the user and does not contain any other rows. Is there some way to do that. Simply deleting the value from title array when index.item gives me a crash. Any idea on it..
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sortedUniqueFirstLetters[section]
}
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sections[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! TableViewCell
cell.account = sections[indexPath.section][indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 80
}
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){
view.tintColor = UIColor.setColor(Red: 223, Green: 220, Blue: 224, Alpha: 1)
let header = view as! UITableViewHeaderFooterView
header.textLabel?.textColor = UIColor.white
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true;
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete{
***if indexPath.item == 0{ //Trying to update title array when a section is empty
sortedUniqueFirstLetters.remove(at: indexPath.section) //Index out of range error here.
}***
sections[indexPath.section].remove(at: indexPath.item)
tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.middle);
}
}
Here is the answer :
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if self.tableView(tableView, numberOfRowsInSection: section) > 0 {
return sortedUniqueFirstLetters[section]
} else {
return nil
}
}
You should also use the heightForHeaderInSction as well along with your titleForHEaderInSction method. Let me know if below code works for you:
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let numberOfRows = tableView.numberOfRows(inSection: section)
if numberOfRows == 0 {
return ""
}else{
return sortedUniqueFirstLetters[section]
}
}
func tableView (tableView:UITableView , heightForHeaderInSection section:Int) -> Float {
let title = self.tableView(tableView, titleForHeaderInSection: section)
if (title == "") {
return 0.0
}
return 80.0
}

How to edit row of cell?

I can not turn on editing mode in the first row of the cell. I tried this code but it didn't help.
public func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
if indexPath.row == 1{
return true
}
return false
}
Can someone help me?
Firstly, make sure your have added UITableViewDataSource protocol. Secondly, you may also need the following implementations.
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let myAction = UITableViewRowAction(style: .normal, title: "MY_ACTION") { (action, indexPath) in
print("I'm here")
}
return [myAction]
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
}
If you want to edit first row then replace
if indexPath.row == 1
with
if indexPath.row == 0
Because indexPath.row starts from 0 not from 1.
Hope that will help.
EDIT:
Since you didn't show your complete code I am adding example code here.
check below code:
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tbl: UITableView!
let arr = ["1", "2", "3"]
override func viewDidLoad() {
super.viewDidLoad()
tbl.dataSource = self
tbl.delegate = self
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tbl.dequeueReusableCell(withIdentifier: "cell")!
cell.textLabel?.text = self.arr[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arr.count
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
//set 0 for first cell
if indexPath.row == 0 {
return true
}
return false
}
//Need this method for delete cell
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete {
tbl.reloadData()
}
}
}