Update tableView from actionController

Hi,


I have a tableView with sections for different OrderItemTypes, and in each row I have a swipe action to change type. What I'm trying to acheive is to reload the data after changing a type, so that the row changes section. As only necessary sections are created when the table is filled the first time, moving rows aren't a viable solution.


I have a tableview with a swipe action, which then calls an actionController. I'm trying to update the tableView after choosing an action from the action sheet, but I don't understand where to put the tableview.reloadData() call.


struct cartViewModel {
  
static func trailingSwipeActionsFor(_ item: orderItem, viewController: UITableViewController ) -> UISwipeActionsConfiguration? {
  var actions = [UIContextualAction]()


  let switchTypeAction = UIContextualAction(style: .normal, title: nil) {  (_,_,success) in

  let ac = UIAlertController(title: "Change type", message: nil, preferredStyle: .actionSheet)
  for type in orderItem.orderItemType.allValues where type != item.type {
  ac.addAction(UIAlertAction(title: "\(type.prettyName)", style: .default, handler: handleChangeType(item: item, type: type)))
  }
  ac.addAction(UIAlertAction(title: "Cancel", style: .cancel))
  viewController.present(ac, animated: true)
  success(true)
  }
  switchTypeAction.backgroundColor = UIColor.preferredFioriColor(forStyle: .accent10b)
nfigureAction.image = FUIIconLibrary.map.marker.job
  actions.append(configureAction)
  }


  let swipeActions = UISwipeActionsConfiguration(actions: actions)
  swipeActions.performsFirstActionWithFullSwipe = false
  return swipeActions

  }

  static func handleChangeType(item: orderItem, type: orderItem.orderItemType) -> (_ alertAction:UIAlertAction) -> (){
  return { alertAction in
  item.type = type
  }
  }

I've tried to call it in "didEndEditingRowAt" and after call to "viewController.present(ac, animated: true)" (line 15 above), but none works. the call to present(ac, animated: true) seems also to be executed aync after "success(true)".

Replies

You don't show where you call trailingSwipeActionsFor.


Anyway, why don't you implement trailingSwipeActionsConfigurationForRowAt.


That's where you should implement the reload, in the action


    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    
        let typeChange = UIContextualAction(style: .normal, title: "Change type") { (action, view, completion) in
            print("Change type")
     //     Change type
            tableview.reloadData()   // reload
            completion(true)
        }
       typeChange.backgroundColor =  UIColor(red: 0.5725490451, green: 0, blue: 0.2313725501, alpha: 1)     // if you want it
         
        let config = UISwipeActionsConfiguration(actions: [typeChange])
        config.performsFirstActionWithFullSwipe = false
    
        return config
    }

I have (sorry), I've just wrapped the config in a separate method. As you can see, I've tried to put it where you suggested (line 15, before success(true), but I need it to update after the action sheet, as that is where the type change is happening.

Anyone? The ActionController seems to be called async by default, so asking for a reload inside the closure doesn't do anything..

Please show more complete code. It is very hard to understand what you changed, how you changed and how everything fits together.


The ActionController seems to be called async by default, so asking for a reload inside the closure doesn't do anything.


Which actionController ?

In which closure ? If you do it that way, should reload in handleChangeType


But once again, please show code (and thanks to take care of identation to make it easier to read).