Wraithseeker
Wraithseeker

Reputation: 1904

UITableViewCell textLabel changing position on selection

I have created a UITableView and a subclass of UITableViewCell using code and it displays perfectly fine untill I select the cell.

The textLabel of the cell would shift rightwards or not shift sometimes

Image Representation of the problem before selection

Image representation of the problem

Image representation of the problem after selection Image representation of the problem after selection

Relevant Code

class MyCell: UITableViewCell {

override func layoutSubviews() {
    super.layoutSubviews()
    // here I fixed the image at a specific point
    self.imageView?.bounds = CGRectMake(0, 0, 100, 125)
    self.imageView!.frame = CGRectMake(0, 0, 100, 125)
    self.imageView?.contentMode = .ScaleAspectFill

} }
 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = MyCell(style: UITableViewCellStyle.Default, reuseIdentifier: "FeedCell")

    self.configureCell(cell, atIndexPath: indexPath)

    return cell
}

func configureCell(cell: MyCell, atIndexPath indexPath: NSIndexPath) {
    let item = self.items[indexPath.row] as MWFeedItem 
    cell.textLabel?.text = item.title 
    cell.textLabel?.font = UIFont.systemFontOfSize(12.0)
    cell.textLabel?.numberOfLines = 0
}

Upvotes: 2

Views: 2234

Answers (1)

Wain
Wain

Reputation: 119041

You should really define your own image view and text label (with different names) in your subclass and apply constraints to lay them out in relation to the size of the cell, then set the row height in the controller so that things are displayed appropriately.

Even in your first image the size of images is incorrect so you have a mismatch configuration between the cell and the controller. And the properties you're currently using should be considered private private in terms of their size and position because you don't own the implementation which sets those features of the views.

Upvotes: 1

Related Questions