how to expand or replace the cell with another cell, when an particular cell select in table view

Viewed 1168

I have already asked this doubt/problem in SO. but not get get solution. Please help me out....

i have one table view which will show the list of name data till 10 datas. But what i need is , when user press any cell, that cell should be replace with another cell, which have some image, phone number, same data name. How to do that.

I have two xib : 1. normalcell, 2. expandable/replace cell

Here is my viewconrolelr.swift

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    
    @IBOutlet weak var tableView: UITableView!
    
    @IBOutlet weak var Resultcount: UILabel!
    
    var tableData = ["thomas", "Alva", "Edition", "sath", "mallko", "techno park",... till 10 data]
    let cellSpacingHeight: CGFloat = 5

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
     
        var nib = UINib(nibName:"customCell", bundle: nil)
        tableView.registerNib(nib, forCellReuseIdentifier: "cell")
        Resultcount.text = "\(tableData.count) Results"
        
        
        
    }
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
         return self.tableData.count
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return cellSpacingHeight
    }
    
    // Make the background color show through
    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UIView()
        headerView.backgroundColor = UIColor.clearColor()
        return headerView
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
     
        
        var cell:customCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! customCell
        
        
        cell.vendorName.text = tableData[indexPath.section]
        
        return cell
        
        
    }


  

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

Starting my cell will look like this :

enter image description here

When i press that cell, i need some thing to do like this with replace ment of like below cell :

enter image description here

But when i press same cell again, again it should go to normal cell.

How to do that ??

2 Answers

You need to create two independent cell on xib. Then you can load using check.You can copy and paste it will work perfectly. in cellForRowAt like this:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if selectedIndexPath == indexPath && self.isExpand  == true{
            let cell = tableView.dequeueReusableCell(withIdentifier: "LeaveBalanceExpandedCell", for: indexPath) as! LeaveBalanceExpandedCell
            cell.delegate = self
            return cell
        }
        else{
            let cell = tableView.dequeueReusableCell(withIdentifier: "LeaveBalanceNormalCell", for: indexPath) as! LeaveBalanceNormalCell
            return  cell
        }
    }
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
//        cell.animateCell(cell)
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
         tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if selectedIndexPath == indexPath{
    if isExpand == true{
        self.isExpand = false
    }
    else{
        self.isExpand = true
    }
    }
    else{
        selectedIndexPath = indexPath
        self.isExpand = true
    }
        self.tableView.reloadData()
    }
Related