# Delete tableviewcell from button within inside the cell

**URL:** <https://forums.kodeco.com/t/delete-tableviewcell-from-button-within-inside-the-cell/121778>\
**Category:** Swift\
**Created:** [October 4, 2020, 11:38pm UTC](https://forums.kodeco.com/t/delete-tableviewcell-from-button-within-inside-the-cell/121778 "2020-10-04T23:38:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![timswift](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/timswift/32/110937_2.png) [@timswift](https://forums.kodeco.com/u/timswift)\
**Post date:** [October 4, 2020, 11:38pm UTC](https://forums.kodeco.com/t/delete-tableviewcell-from-button-within-inside-the-cell/121778/1 "2020-10-04T23:38:39Z")

</div>

I want my swift code to delete the tableview cell within the cell using the blue button. You can see a example of what I am trying to below in the gif. I also have a indexPath selection var. You should probably have to delete it in the deleteCell func also I assume you would use a tag. All the code is in below no need for storyboard.

```
  import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource, UIImagePickerControllerDelegate & UINavigationControllerDelegate {
    var arr = [1,1,3,3]
    var tableview = UITableView()
    var selectedIndexPath = IndexPath(row: 0, section: 0)
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 118
    }

   
    
    
   
    
    
    
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! customtv
      
  
     
        
        return cell
    }
    
    @objc func deleteCell(){
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        tableview.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height * 0.8)
      
        view.addSubview(tableview)
    

     
        tableview.register(customtv.self, forCellReuseIdentifier: "cell")
        tableview.delegate = self
        tableview.dataSource = self

      
 
        
    }
    
    
    
}

class customtv: UITableViewCell {
    lazy var backView : UIView = {
        let view = UIView(frame: CGRect(x: 10, y: 6, width: self.frame.width , height: 110))
        view.backgroundColor = .green
        
        return view
    }()
    
    lazy var btn : UIButton = {
        let btn = UIButton(frame: CGRect(x: 50, y: 6, width: 100 , height: 110))
        btn.backgroundColor = .blue
        return btn
        
    }()
    
    
    
    override func layoutSubviews() {
        backView.clipsToBounds = true
        backView.frame = CGRect(x: 0, y: 6, width: bounds.maxX , height: 110)
        
        
    }

    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(animated, animated: true)
        addSubview(backView)
        backView.addSubview(btn)

    }
    
    
    
}

```

---

<div class="post-metadata">

**Author:** ![jayantvarma](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jayantvarma/32/111768_2.png) [@jayantvarma](https://forums.kodeco.com/u/jayantvarma)\
**Post date:** [October 10, 2020, 2:52am UTC](https://forums.kodeco.com/t/delete-tableviewcell-from-button-within-inside-the-cell/121778/2 "2020-10-10T02:52:15Z")

</div>

@timswift there is no gif with this post.

If you are wanting to add a tableView as an element as you would add a label or an image to a cell, then you need to create your tableView outside as a component that can be initialised just as you would a label or an image.

cheers,

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [March 20, 2021, 3:38pm UTC](https://forums.kodeco.com/t/delete-tableviewcell-from-button-within-inside-the-cell/121778/3 "2021-03-20T15:38:44Z")

</div>

Closed on March 20, 2021, 3:38pm UTC

This topic was automatically closed after 166 days. New replies are no longer allowed.
