Mad Burea
Mad Burea

Reputation: 541

Add Custom Cell inside Custom Cell

I have an interesting problem. as i am new to Swift.

I have created on TableView and added CUSTOM CELL using Storyboard. Now i want add another CUSTOM CELL When click on first CUSTOM CELL UIButton.

Second Custom Cell is created using XIB. now when i register That second Cell in didload then i see blank tableview as Second custom cell is Blank.

i have used following Code:

for registering Second cell

   self.tableView.registerNib(UINib(nibName: "customCell", bundle: nil), forCellReuseIdentifier: "customCell")

and cell for row at index

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

        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! Cell

        cell.nameLbl.text = "Hello hello Hello"

        let Customcell = tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as! customCell


        if self.Selected == "YES" {
            if self.selectedValue == indexPath.row {


                return Customcell
            }

            return cell

        }
        else{

            return cell
        }
    }

Here Cell object is for Storyboard Cell and Customcell is for XIB Second custom cell.

please suggest me how to do that.

Upvotes: 0

Views: 308

Answers (1)

Joshua Finley
Joshua Finley

Reputation: 31

First ensure your ViewController is the UITableViewDelegate and UITableViewDataSource of the tableView, and that you have an outlet for the tableView

Next you need to register the custom cell in the viewDidLoad method:

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.register(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "customCell")
}

It is easiest to save an array of cells that have been selected if you are going to have more than 1 cell that you want to modify when pressed. This can be a variable within the ViewController:

var customCellIndexPaths: [IndexPath] = []

when a cell is selected you can simply add it to the array of custom cell IndexPaths (if it is not already a custom cell), and then reload that cell:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if customCellIndexPaths.contains(indexPath) == false {
        customCellIndexPaths.append(indexPath)
        tableView.reloadRows(at: [indexPath], with: .automatic)
    }
}

In the cellForRowAt method we must check whether the cell has been selected and if so return the custom cell, else return a normal cell:

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

    if customCellIndexPaths.contains(indexPath) {
        return tableView.dequeueReusableCell(withIdentifier: "customCell")!
    }

    let cell = UITableViewCell(style: .default, reuseIdentifier: "normalCell")
    cell.textLabel?.text = "Regular Cell"
    return cell
}

There you have it. Now you should receive a smooth animation of a normal cell becoming a CustomCell when being selected.

Upvotes: 1

Related Questions