fandro fandro - 7 months ago 25
Swift Question

Add a row before rendering tableview swift 2

I am trying to add a row into my tableview before the

tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
method is called. I tried to do it into the
viewDidLoad()
method without success is it possible ? and how ?

Here is my code :

import UIKit

class CustomTestViewController : UITableViewController {

@IBOutlet var formDetailTableView: UITableView!
var data: Data?
var firstData: FirstData?

struct CurrentFormTableView {
struct CellIdentifiers {
static let MyCell = "MyCell"
static let MyFirstCell = "MyFirstCell"
}
}

override func viewDidLoad() {
super.viewDidLoad()
//HERE ADD THE FIRST ROW ?
let cell = tableView.dequeueReusableCellWithIdentifier(CurrentFormTableView.CellIdentifiers.MyFirstCell, forIndexPath: indexPath) as! MyFirstCell
cell.displayCell(firstData)
return cell
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(CurrentFormTableView.CellIdentifiers.MyCell, forIndexPath: indexPath) as! MyCell
cell.displayCell(data[indexPath.row])
return cell
}

}

Answer

If I understand your question properly this problem can be easily :)

You always wants to show a first cell in your tableView no matter whether there is a data in your data array or not :) and your problem is you cant add the first object to data array thats all right buddy :)

Here is a solution :)

Don't do anything in ViewDidLoad :) Simply save your first row data object in your local variable lets say : yourCustomObject

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count + 1
    }

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell : myTestCell = tableView.dequeueReusableCellWithIdentifier("testCell")! as! myTestCell

        if indexPath.row == 0 {
             cell.nameLabel.text = yourCustomObject.property
        }
        else {
            cell.nameLabel.text = data[indexPath.row -1].property
        }
        return cell
    }

Problem solved :) Happy coding :)

How It works : Simple,

Assume your data array is empty :) then it will return count as 0 :) But you always want to show your first cell isn't it :) so add +1 to data array count :) return data.count + 1

Now handle it carefully in cellForRowAtIndexPath. You dont want to end up accessing data in data array for first object so check for indexpath 0.

And you dont want to end up accessing object out of index of data so use data[indexPath.row -1]

Hope I made my point clear :) Happy coding