Nick89 Nick89 - 2 months ago 12
Swift Question

Align cell textLabel to top left programmatically in swift

I have programmatically created a TableView using sections in swift. The amount of sections and rows will always be the same, as they're just displaying info that I pull in from Firebase.

I would like to change the textLabel of one of the cells to align to the top, instead of the middle of the cell. All other textLabels I would like to remain centered. The cell in question is section 1, row 1.

It currently looks like:

enter image description here

I've been looking for how to do this for a while, and a lot of people say to do:

cell.textLabel?.numberOfLines = 0
cell.textLabel?.sizeToFit()


But this isn't working. I'm placing this code in my
cellForRowAtIndexPath
method.

if indexPath.section == 0 {
if indexPath.row == 0 {
cell.textLabel!.text = "Our review"
} else {
cell.textLabel!.text = film?.Main_Review
cell.textLabel?.numberOfLines = 0 // HERE
cell.textLabel?.sizeToFit() // HERE
}
}


Nothing happens, the text still aligns to centre of the cell.

Does anyone know a simple way of doing this? Preferably without having to create a custom cell (as this only needs to effect 1 row)? Thanks

UPDATE - All code for
cellForRowAtIndexPath


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell: UITableViewCell = UITableViewCell(style: .Value1, reuseIdentifier: cellId)

cell.selectionStyle = .None
cell.textLabel?.font = UIFont(name: "Verdana", size: 14)
cell.textLabel?.textColor = UIColor.darkGrayColor()
cell.detailTextLabel?.font = UIFont(name: "Verdana", size: 14)


if indexPath.section == 0 {
if indexPath.row == 0 {
cell.textLabel!.text = "Share"
}
} else if indexPath.section == 1 {
if indexPath.row == 0 {
cell.textLabel!.text = "Our review"
} else {
cell.textLabel!.text = film?.Main_Review
cell.textLabel?.numberOfLines = 0
cell.textLabel?.sizeToFit()
}
} else if indexPath.section == 2 {
if indexPath.row == 0 {
cell.textLabel!.text = "UK release date"
cell.detailTextLabel!.text = film?.Date_Released?.capitalizedString
} else if indexPath.row == 1 {
cell.textLabel!.text = "Directed by"
cell.detailTextLabel!.text = film?.Directed_By?.capitalizedString
} else if indexPath.row == 2 {
cell.textLabel!.text = "Running time"
cell.detailTextLabel!.text = film?.Running_Time
} else {
cell.textLabel!.text = "Certificate rating"
cell.detailTextLabel!.text = film?.Certificate_Rating
}
}

return cell

}

Answer

cell.textLabel is a default label in UITableViewCell. If you want your text to start at the top you should create a subclasse of UITableViewCell and create your own view for this cell. For example

import Foundation
import UIKit

class ItemCell: UITableViewCell {

var label : UILabel!

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    self.label = UILabel()
    self.contentView.addSubview(label)
}

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

override func layoutSubviews() {
    super.layoutSubviews()

    self.label.frame = CGRectMake(15,10,200,30)
    self.label.adjustsFontSizeToFitWidth = true
    self.label.textColor = UIColor.greenColor()

}

}

Then in your viewController dont forget to use the register class method like this

    self.tableView.dataSource = self
    self.tableView.delegate = self
    self.tableView.registerClass(ItemCell.self, forCellReuseIdentifier: "cellIdentifier")

And in your cellForRowAtIndexPath

let cell:ItemCell! = tableView.dequeueReusableCellWithIdentifier("cellIdentifier") as? ItemCell