greenpoison oneTV greenpoison oneTV - 1 month ago 7
Swift Question

Set last tableView cell as constant cell among variable cells in Swift

As the title says, I would like to create a cell which is constant in a tableView where cells are variable. I want this cell to be the last. Actually, all my cells are created with data from Firbase. The last constant cell should be always there. Thx for the help.

Answer

You can try something like this:

You can change only cell at some indexPath (last in your case) and set it how you want to.

Here is an example:

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

        if ((indexPath as NSIndexPath).row == 8 {

        let cell = tableView.dequeueReusableCell(withIdentifier: "lastCell", for: indexPath) as! lastCellClass

          //your code

          return cell

        }else{

           let cell = tableView.dequeueReusableCell(withIdentifier: "dataCell", for: indexPath) as! dataCellClass

          //your code

          return cell

        }


    }

Do not forget that cells are zero based (indexPath.row 8) is 9th cell.

Note: If your numberOfRows can change you can do:

 if ((indexPath as NSIndexPath).row == (yourData.count - 1){ 
   //Here you will get your last cell

}
Comments