I'm trying to implement a UITableViewController.

I wrote some code for a custom cell class and a UITableViewController and I have 3 questions for the custom cell:

1) How can I modify the properties of the string "title" and "description" in the titleView and descriptionView such as the size, font, color, etc?

2) How can I set the Image to stay on the left side and the Title of the image to be on the right side while both of them are above the description field? Here is a screenshot with the current output

3) In my titleView, there is a problem when I run the app meaning that any user could modify the title, this problem doesn't appear in the description field. Can anyone figure out why? I will also add a screenshot below with this problem: Anyone can modify the title while using the app

Here is the code for the UITableViewController:

struct CellData {
    var title: String?
    var description: String?
    var image: UIImage?

class TableViewController: UITableViewController {

    var data = [CellData]()

    override func viewDidLoad() {
        data = [CellData.init(title: "Dips", description: "Dips is one of the best exercises when it comes to building chest, triceps and even side delts. It's also good in the long run due to the fact that it implies your core a lot.", image: #imageLiteral(resourceName: "Dips"))]
        self.tableView.register(CustomCell.self, forCellReuseIdentifier: "custom")

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCell(withIdentifier: "custom") as! CustomCell
        cell.mainImage = data[indexPath.row].image
        cell.myDescription = data[indexPath.row].description
        cell.title = data[indexPath.row].title
        cell.layoutSubviews() // you don't have to scroll up to scale them properly
        return cell

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count

Here is the code for the custom cell:

    class CustomCell: UITableViewCell {

    var title: String?
    var myDescription: String?
    var mainImage: UIImage?

    var titleView: UITextView = {
        var mainTitleView = UITextView()
        mainTitleView.translatesAutoresizingMaskIntoConstraints = false
        return mainTitleView

    var myDescriptionView: UITextView = {
        var textView = UITextView()
        textView.translatesAutoresizingMaskIntoConstraints = false // set this to false if you plan to add future constraints
        textView.isScrollEnabled = false
        return textView

    var mainImageView : UIImageView = {
        var imageView = UIImageView()
        imageView.translatesAutoresizingMaskIntoConstraints = false // same applies here
        imageView.contentMode = .scaleAspectFit
        return imageView

    // you just type init
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)


        titleView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
        titleView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
        titleView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
        titleView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true

        myDescriptionView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
        myDescriptionView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
        myDescriptionView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true

        mainImageView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
        mainImageView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
        mainImageView.bottomAnchor.constraint(equalTo: self.myDescriptionView.topAnchor).isActive = true
        mainImageView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
        mainImageView.heightAnchor.constraint(equalToConstant: 120).isActive = true

    // just type layout and Pick
    override func layoutSubviews() {

        if let title = title {
            titleView.text = title
        if let description = myDescription {
            myDescriptionView.text = description
        if let image = mainImage {
            mainImageView.image = image

        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder: ) has not been implemented")

0 Answers