0

I have a TableViewController with 3 sections with their own headers. Now I want before inserting any cell, check a property and then add the cell into different sections.

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    // Table view cells are reused and should be dequeued using a cell identifier.
    let cellIdentifier = "TasksTableViewCell"


    guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? TasksTableViewCell  else {
        fatalError("The dequeued cell is not an instance of TasksTableViewCell.")
    }

    // Fetches the appropriate task for the data source layout.
    let task = tasks[indexPath.row]

    cell.nameLabel.text = task.name
    cell.photoImageView.image = task.photo
    cell.timeControl.text = task.lengthDisplay.replacingOccurrences(of: "Length: ", with: "")

    if(task.importanceLevel == 0){
         // add cell to section 0
    }
    else if(task.importanceLevel == 1){
         // add cell to section 1
    }

    // Configure the cell...

    return cell
}

Can u see the comment, is there any way to do that? Thank you very much

  • I think you should change your data model. You need to determine the number of rows in section by 'importanceLevel' parameter for each task. 'cellForRowAt' method should just display proper cell for each row in section. Filtering data must be done before that – Sasha Kozachuk Dec 21 '16 at 10:12
  • http://blog.apoorvmote.com/uitableview-with-multiple-sections-ios-swift/ this is basic please go through... – Arun Dec 21 '16 at 10:14
  • Is there any sample of this? Thanks – Quang Dinh Luong Dec 21 '16 at 10:15
  • http://stackoverflow.com/questions/29578965/how-do-i-populate-two-sections-in-a-tableview-with-two-different-arrays-using-sw https://gist.github.com/mchirico/50cdb07d20b1b0f73d7c https://www.youtube.com/watch?v=Ifm-Tvzz31E – Arun Dec 21 '16 at 10:16
  • Still not what I want :((. But I do change sth: section = ['a', 'b', 'c'] and successfully display these sections. I want to do this: when check if (task.importanceLevel = 0) then add cell to section 'a', so on and so on – Quang Dinh Luong Dec 21 '16 at 11:12

3 Answers3

0

You can create a model and pass empty count arrays at first for each section and row in data source methods. And then initialize the count and fill arrays their then reload your table view. I hope you know there is numberOfSections method too.

amber
  • 31
  • 9
0

I think this code will help you:

let arr = [5,2,7,10,2]
override func viewDidLoad() {
    super.viewDidLoad()
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 5
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return arr[section]
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("celldata", forIndexPath: indexPath)

    cell.textLabel?.text = "section \(indexPath.section)"
    cell.detailTextLabel?.text = "Rows \(indexPath.row)"

    return cell
}
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = UIView(frame: CGRect(x: 0, y: 0, width: self.tableView.frame.width, height: 20))
    let lbl = UILabel(frame: CGRect(x: 5, y: 5, width: self.tableView.frame.width, height: 15))
    lbl.text = "\(section)"
    view.backgroundColor = UIColor.grayColor()
    view.addSubview(lbl)
    return view
}
Ronak Adeshara
  • 321
  • 4
  • 13
-2

You can try it like this

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


    if(indexPath.section == 0){
        let cellIdentifier = "TasksTableViewCell"

        guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? TasksTableViewCell  else {
            fatalError("The dequeued cell is not an instance of TasksTableViewCell.")
        }

     // Fetches the appropriate task for the data source layout.

        let task = tasks[indexPath.row]
        cell.nameLabel.text = task.name
        cell.photoImageView.image = task.photo
        cell.timeControl.text = task.lengthDisplay.replacingOccurrences(of: "Length: ", with: "")

        return cell
    }
    else if(indexPath.section == 1)
        let cellIdentifier = "TasksTableViewCell1"

        guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? TasksTableViewCell1  else {
            fatalError("The dequeued cell is not an instance of TasksTableViewCell.")
        }

     // Fetches the appropriate task for the data source layout.
        let task = tasks[indexPath.row]
        cell.nameLabel.text = task.name
        cell.photoImageView.image = task.photo
        cell.timeControl.text = task.lengthDisplay.replacingOccurrences(of: "Length: ", with: "")

        return cell
    }
}
Ankita Shah
  • 2,178
  • 3
  • 25
  • 42
  • Thanks for ur help. But you know task.importanceLevel is accessible only after let task = tasks[indexPath.row] right? And I don't have TasksTableViewCell1, I just have 3 different sections that I want to add the cell to – Quang Dinh Luong Dec 21 '16 at 10:46
  • @QuangDinhLuong I had updated the code. You can load the cell based on current active section – Ankita Shah Dec 22 '16 at 04:48