1

Here is a visual of what I am working with:

image of view hierarchy *

I'm looking to put a label titleLabel in the outermost view "UIView" that stays positioned above a subview in the table view cell (In the image, it would be the UIButton). My method was to constrain the Y value of titleLabel in IB, and set the center x value of titleLabel to the center x value of UIButton (using convertPoint).

So just to clarify:

//    ->  UIView
//    ->->  titleLabel-------- This matches center.x's with...
//    ->->  tableView
//    ->->->  tableViewCell
//    ->->->->  UIButton------ ...this

I can get it to work by putting the code here, but it only moves the titleLabel when it needs to load a new table cell, and it doesn't do it when the view initially loads which seems to be my problem:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if let cell = tableView.dequeueReusableCellWithIdentifier("newCell") as? newCell {
        cell.configureCell(myArray[indexPath.row])
        let centerPoint = cell.myButton.convertPoint(cell.myButton.center, toView: self.view)
        titleLabel.center.x = centerPoint.x - (titleLabel.frame.width / 2)
        return cell
    } else {
        let cell = newCell()
        let centerPoint = cell.myButton.convertPoint(cell.myButton.center, toView: self.view)
        titleLabel.center.x = centerPoint.x - (titleLabel.frame.width / 2)
        return cell
    }
}

So I'm just wondering where I can put this code so that it runs before anything is displayed? Or if anyone has a better method, I'd really appreciate the help. For anyone wondering, it's so that the title will stay above certain elements in the cell regardless of the size of the screen/table/etc. Thanks so much in advance.

P.S. I realize this also probably isn't the most efficient way to get it to work, but I'm just feeling things out first. Any tips here are very appreciated as well

*Image borrowed from this question on SO: Using convertPoint to get the relative position inside a parent UIView

Community
  • 1
  • 1
Sean
  • 169
  • 9

0 Answers0