-1

I try to add constraints to label added to Basic Style Cell in UITableView, but they are not active.

I use Basic Style instead of Custom, because I need some rows start from Title and another from cell.ImageView!.image

If I use Custom cell, when there is no ImageView, Title is anyway located far from left side of the row.

Screenshot:

Screenshot

curious
  • 1,504
  • 5
  • 18
  • 32
O.Soap
  • 9
  • 4
  • **Kindly** ,you should show us what you tried and how it failed. I recommend you see [this](https://youtu.be/H9NhYx9xIiU?t=273) video and also see [this](https://stackoverflow.com/questions/18969355/how-to-create-a-custom-uitableviewcell-programmatically-using-autolayout). You can also search more for "how to create a tableViewcell programatically – mfaani Aug 24 '17 at 00:08
  • I failed because I can not find any constraint with condition of existing neighbor object or not and because Basic Style Cell constraints are not active and I dont know why. – O.Soap Aug 24 '17 at 19:00

2 Answers2

1

Use fully custom cell. Don't even bother with the predefined one. Build it by yourself, and you wont have to worry about anything and you can use constraints as you wish (just make sure these are related to content view of the cell, not any other view - UITableViewCell has quite a few of them).

Vojta Rujbr
  • 325
  • 3
  • 11
  • Thank you for answer, but I have no idea which constraint will make title shift to left side of row, if there is no ImageView. I tried to stick title to image view with leading space to it, but no deal. – O.Soap Aug 24 '17 at 00:03
0

I found answer in Apple Documentation: https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/ProgrammaticallyCreatingConstraints.html

And this code is working:

import UIKit

class ViewController: UITableViewController {


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

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

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 4
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ChecklistItem", for: indexPath)

    let label = cell.viewWithTag(1000) as! UILabel
    let image = cell.viewWithTag(1001) as! UIImageView
    let margins = cell.contentView.layoutMarginsGuide

    if indexPath.row == 0 {
        label.text = "First row"
    } else if indexPath.row == 1 {
        label.text = "Second row"
        image.image = UIImage(named: "test")
    } else if indexPath.row == 2 {
        label.text = "Third row"
        label.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true

    } else if indexPath.row == 3 {
        label.text = "Fourth row"
    }
    return cell
    }
}

Screenshot

O.Soap
  • 9
  • 4