Search code examples
iosswiftuitableviewdynamiccell

How can I order two dynamic table view cells in swift?


Greeting everyone!

I am a noob when it comes to swift or other programming language, but I do want to learn. I am working on an app that, basically, will display a page of Results in a dynamic tableview. I need the table to have two different type of cells (shown bellow with two identifiers: cell and kidcell). "Cell" is "static", its information is set in code and doesn't change. It also segues to a different view controller, showing the user other info. "kidcell" is changed dynamically according to the info passed to it from a different page.

My problems is that "kidcell" cells are shown on top of the "cell" one. How can I reorder those two type of cells in code, in swift?

Since this is done only in code, the storyboard doesn't have any "prototype" cells in this table view. Nevertheless, I tried to create two, giving each one of the two identifiers I need, and arrange those, but that still doesn't work.

In storyboard, I also added different "tags" to the two cells, still no luck.

Please help me, I feel the solution is very simple but for the life of me I can't figure it out and I can't find it anywhere.

EDIT:

"cell" will never change, as I mentioned "kidcell" cells will always change, the data in them and also the number of "kidcell" cells, depending on the data passed. I want:

  1. cell - on top
  2. kidcell - below, with whatever cells will be.

Thank you!

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
    if ((indexPath.section == 0 && indexPath.row == boys.count) || (indexPath.section == 1 && indexPath.row == girls.count)) {
        
        var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("cell") as? UITableViewCell
        
        if (cell == nil) {
            
            cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
            cell?.accessoryType = .DisclosureIndicator
            
        }
        
        cell?.textLabel?.text = (indexPath.section == 0) ? "Boys" : "Girls"
        
        return cell!
        
    } else {

        var kidcell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("kidcell") as? UITableViewCell
        
        if (kidcell != nil) {
        
            kidcell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "kidcell")

        }

Solution

  • You need NSArray that holds all the cells in your tableview. The first object should be "Cell", and the other should be "kidCell". Just retrieve the cell in the cellForIndexPath by the index of the row.