Add view to cell in table view programmatically

I have a table view with dynamic prototypes that currently displays 4 items programmatically. I am assigning cell names through an array (see code below), but I also want to add a small view to the right half of each cell to show a small graph next to each label. I can take a look in the storyboard, but it doesn't propagate dynamically. Basically I need to find a way to modify this code to add a view inside each cell and assign the class I already created for those views.

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("LineCell", forIndexPath: indexPath) as! UITableViewCell
    cell.textLabel?.text = oneLine.lineNames[indexPath.row]
    return cell
}

      

This is where I need views:

enter image description here

Also, how can I make more space between Carrier, Time, Battery and the table view?

+4


source to share


3 answers


You can create the view however you want and add it as a subhead of your cellView content:



var newView = UIView(frame: CGRectMake(200, 10, 100, 50))
cell.contentView.addSubview(newView)

      

+8


source


To add a "Space" between your table view and status bar, you can embed your view controller in the uinavigationconttoller.



To add a view dynamically, you can use the addSubview method on your cell and add constraints to your code with auto layout

+3


source


Swift 4.0

var newView = UIView(frame: CGRect(x: 210, y: 10, width: 110, height: 50))
cell.contentView.addSubview(newView)

      

+1


source







All Articles