0

I have a few custom tableview cells which all have a different unib and different classes and want to reorder the cells and persistent safe their reordered position.

I have registered every cell and UINib in my TablevViewController

    let test1Nib = UINib(nibName: "Test1", bundle: nil)
    myTableView.register(overViewNib, forCellReuseIdentifier: "Test1")

    let test2Nib = UINib(nibName: "Test2", bundle: nil)
    myTableView.register(todoNib, forCellReuseIdentifier: "Test2")

    let test3Nib = UINib(nibName: "Test3", bundle: nil)
    myTableView.register(financeNib, forCellReuseIdentifier: "Test3")

    let test4Nib = UINib(nibName: "Test4", bundle: nil)
    myTableView.register(upcomingNib, forCellReuseIdentifier: "Test4")

I have then added the UINibs to my testArray:

    testArray.append(test1Nib)
    testArray.append(test2Nib)
    testArray.append(test3Nib)
    testArray.append(test4Nib)

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if indexPath.row == 0 {
        let cell = myTableView.dequeueReusableCell(withIdentifier: "Test1", for: indexPath)
        return cell
    } else if indexPath.row == 1 { 
        let cell = myTableView.dequeueReusableCell(withIdentifier: "Test2", for: indexPath)
        return cell
    } else if indexPath.row == 2 {
        let cell = myTableView.dequeueReusableCell(withIdentifier: "Test3", for: indexPath)
        return cell
    } else {
        let cell = myTableView.dequeueReusableCell(withIdentifier: "Test4", for: indexPath)
        return cell
    }

}

The moveRowAt: function is doing as expected

func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
    let movedObject = self.testArray[sourceIndexPath.row]
    testArray.remove(at: sourceIndexPath.row)
    testArray.insert(movedObject, at: destinationIndexPath.row)
    NSLog("%@", "\(sourceIndexPath.row) -> \(destinationIndexPath.row)")
}

At this point I got a few issues 1. after reorder the cells are reloaded since I have fixed their index path to a static value and well now the questions is how can I prevent them to reorder them selfs and 2. How can I persist this order

I hope I can find help here - I researched the web a lot now but nothing suites my needs. Hope I can find a solution or some links or code snippets. thanks a lot!

Michael
  • 5
  • 4

1 Answers1

0

You should not force the check of indexPath row to decide which cell to load.

You should get their type and then switch into it. something like:

let type = testArray[indexPath.row]
switch type {
    case Type1:
    let cell = myTableView.dequeueReusableCell(withIdentifier: "Test1", for: indexPath)
     return cell

    case Type2:
    let cell = myTableView.dequeueReusableCell(withIdentifier: "Test2", for: indexPath)
    return cell
}

How you set this type depends on you. There are several ways to do this, pick your favorite.

gmogames
  • 2,993
  • 1
  • 28
  • 40
  • thanks for your quick answer! that worked as expected - now i need to deal with the persistance of the cells - but thats another topic ;-) Thanks a lot! – Michael Mar 27 '18 at 07:40
  • @Michael your welcome. Please select this answer as the correct one and upvote. Thanks! – gmogames Mar 27 '18 at 10:05
  • @gnogames already upvoted but since i'm new here at Stackoverflow and have less than 15 rep it wont display for public but it counts ;-) – Michael Mar 27 '18 at 11:29