0

I Have a UITableView which is controlled by NSFetchedResultsController. I want to add single cell to the first row and make this cell static. In other words, there will be a button which will open another View Controller.

Until now, I was ok with fetched results controller and table. Now I'm a bit confused. How should I do this?

Instead using a header might be ok too, but I don't want this header to be on top all the time. I want this cell to be just like WhatsApp iOS "Create new group" cell on chats panel.

Thank you!

Amanpreet
  • 1,301
  • 3
  • 12
  • 29
anyName
  • 113
  • 1
  • 2
  • 13
  • The simplest way of doing this is Goto UITableView -> Drag a UIView onto it above your regular cell as Header View, add button on it and action in the ViewController, it will be always on top no need to write any code to handle this. – iphonic Jan 17 '17 at 07:30
  • Instead of creating a cell you should add a button on in table view's header. – User511 Jan 17 '17 at 07:35
  • Adding a view is the best one! Thank you @iphonic I don't know why i did not think this. Thank you again! – anyName Jan 17 '17 at 07:46

2 Answers2

0

You will need to create tableview with number of rows fetched from NSFetchedResultsController +1. Also in cellForRowIndex method you will need to add a check like indexPath.row == 0 and in there you will make the changes.

Also you will have to add action for that button within that section. You can also set different custom tableview for first row. It can be similar to following:

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

  if(indexPath.row==0){
     let cell = tableView.dequeueReusableCell(withIdentifier: "CellWithButton", for: indexPath) as! CellWithButton
  }
  else{
      let cell = tableView.dequeueReusableCell(withIdentifier: "OtherCells", for: indexPath) as! OtherCells
      //here add data for cells from your array
   }  
 return cell
}
dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
pankaj
  • 7,878
  • 16
  • 69
  • 115
0
var dataArray = ["A","B","C"]

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.dataArray.count+1
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    if indexPath.row == 0
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CreateNewGroupCell") as! CreateNewGroupCell
        return cell
    }
    else
    {
       // Get the data from Array
          let data = self.dataArray[indexPath.row-1]

       // Logic to show other cells
          let cell = tableView.dequeueReusableCell(withIdentifier: "OtherCell") as! OtherCell
          return cell

       // ....
    }

}
Suresh Peddisetti
  • 3,782
  • 3
  • 23
  • 26