subash chandru
subash chandru

Reputation: 49

I cannot able to add subview to UItableviewcell

Code

 import UIKit

 class ViewController: 
 UIViewController,UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 3
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var suma=UITableViewCell()
    var sampleviewpurple=UIView(frame: CGRect(x: suma.frame.width/0, y: 0, width: suma.frame.width/2, height: suma.frame.height))
    sampleviewpurple.backgroundColor=#colorLiteral(red: 0.2196078449, green: 0.007843137719, blue: 0.8549019694, alpha: 1)

    var sampleviewred=UIView(frame: CGRect(x: suma.frame.width/1, y: 0, width: suma.frame.width/2, height: suma.frame.height))
    sampleviewred.backgroundColor=#colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)



    suma.contentView.addSubview(sampleviewpurple)
    suma.contentView.addSubview(sampleviewred)

    return suma

}




@IBOutlet weak var tab: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()
    tab.dataSource=self
    tab.delegate=self

}

  }

when I try to add a subview to the suma and return to the delegate, My table view is still blank, I am returning valid data source to the data source when and also noticed the reference to the table view all is fine, But still how I can not able to add a subview to the uitableviewcell

Upvotes: 2

Views: 1900

Answers (4)

Swetha Lakshmi
Swetha Lakshmi

Reputation: 2019

I made three change.

  1. Dequeue the cell for reusability.

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var suma = UITableViewCell()
    suma = tab.dequeueReusableCell(withIdentifier: "DefaultCell")!`
    
  2. Changed the sampleviewpurple and sampleviewred x position

    let sampleviewpurple = UIView(frame: CGRect(x: 0, y: 0, width: suma.frame.width/2, height: suma.frame.height))
    
    let sampleviewred = UIView(frame: CGRect(x: suma.frame.width/2, y: 0, width: suma.frame.width/2, height: suma.frame.height))
    
  3. Registered the cell which is created programmatically to create a connection between UITableView and UITableViewCell in viewDidLoad function

    tab.register(UITableViewCell.self, forCellReuseIdentifier: "DefaultCell")
    

Upvotes: 0

Shubham
Shubham

Reputation: 773

For setting your view's frame you are using the cell's frame which is not set yet. You should use such a view which frame is already set and on which bases you want to calculate the frame of your new views.

Try this:

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var sumaCell:UITableViewCell?
           sumaCell = tableView.dequeueReusableCell(withIdentifier: "sumaCell")
    if  sumaCell == nil {
        sumaCell = UITableViewCell(style: .default, reuseIdentifier: "sumaCell")
    }




    var sampleviewpurple=UIView(frame: CGRect(x: 0, y: 0, width: ((sumaCell?.frame.width)!/2), height: (sumaCell?.frame.height)!))
    sampleviewpurple.backgroundColor=#colorLiteral(red: 0.2196078449, green: 0.007843137719, blue: 0.8549019694, alpha: 1)

    var sampleviewred=UIView(frame: CGRect(x: (sumaCell?.frame.width)!/2, y: 0, width: ((sumaCell?.frame.width)!/2), height: (sumaCell?.frame.height)!))
    sampleviewred.backgroundColor=#colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)

    sumaCell?.contentView.addSubview(sampleviewpurple)
    sumaCell?.contentView.addSubview(sampleviewred)

    return sumaCell!

}

Upvotes: 0

StuckPixel
StuckPixel

Reputation: 111

You are creating the cell in code. First of all, you have to register the UITableViewCell in viewDidLoad method.

tab.register(UITableViewCell.self, forCellReuseIdentifier: "DefaultCell")

Then you have to use the dequeue cell in cellForRowAt method.

var suma = tab.dequeueReusableCell(withIdentifier: "DefaultCell")

If you are instantiating the cell from storyboard, you just have to use dequeue cell. No need to register.

Upvotes: 0

qtngo
qtngo

Reputation: 1679

In the cellForRowAt function, you must dequeue your table cell, not instantiate UITableViewCell:

var suma = tableView.dequeueReusableCell(withIdentifier: "YOUR_CELL_IDENTIFIER")

Upvotes: 1

Related Questions