-1

In my UITableViewCell's content view I have a label that I want to set the frame of programmatically, but it just stays the same no matter what frame I give it. My cellForRowAtIndexPath-function looks like this:

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


    let selectedBackground = UIView(frame: CGRectMake(0, 0, width, 44))
    let selectedBackgroundImage = UIImageView(frame: CGRectMake(width - 20, 44/2 - 5, 10, 10))
    selectedBackgroundImage.image = UIImage(named: "checked")
    selectedBackgroundImage.contentMode = .ScaleAspectFit
    selectedBackground.addSubview(selectedBackgroundImage)


    let separatorViewRegular:UIView = UIView(frame: CGRectMake(0, 43, 1024, 1))
    separatorViewRegular.layer.borderColor = grayColor.CGColor
    separatorViewRegular.layer.borderWidth = 1

    let cell:CompanyTableViewCell = self.companiesTV.dequeueReusableCellWithIdentifier("cell") as! CompanyTableViewCell

        cell.label.textColor = UIColor(red: 100/255, green: 100/255, blue: 100/255, alpha: 1)
        cell.label.frame = CGRectMake(0.7 * width, 0, 0.3 * width, 44)
        cell.label.font = UIFont(name: "Avenir", size: 16)
        cell.label.adjustsFontSizeToFitWidth = true
        cell.label.numberOfLines = 1
        cell.label.textAlignment = NSTextAlignment.Left

        cell.backgroundColor = UIColor.clearColor()

        cell.label.text = tickersToDisplay[indexPath.row]

        cell.selectedBackgroundView = selectedBackground
        cell.contentView.addSubview(separatorViewRegular)

        return cell

}

Any help and suggestion on why this doesn't work would be appreciated.

narner
  • 2,908
  • 3
  • 26
  • 63
joseph
  • 897
  • 2
  • 10
  • 20
  • My suggestion is don't do this in cellForRowAtIndexPath. For one thing, you'll be adding subviews to cells that already have them when the cell is reused. You should add any subviews in the cell's init method. – rdelmar Mar 16 '15 at 20:30
  • @rdelmar As far as my knowledge goes, I'm not directly adding any subviews, I'm simply trying to adjust the frame of the content I've already added in my nib-file. – joseph Mar 16 '15 at 20:33
  • @rdelmar If you're thinking about the adding of 'separatorViewRegular' that will not become a problem, since it's supposed to be in all cells no matter what. – joseph Mar 16 '15 at 20:34
  • Where did you make the cell's subviews in the first place? In IB, or in code? Why not give it the correct frame to start with; why do you want to change it in cellForRowAtIndexPath if the change is not dependent on the indexPath? You're putting a lot of code in cellForRow (which is called every time you scroll) that only needs to run once, so putting in the cell's class makes more sense. – rdelmar Mar 16 '15 at 21:01
  • @rdelmar I made the subviews in IB, the frame changes depending on if it's in portrait, landscape, on an iPad etc. I could've put it in the cells class, but that still don't solve the issue I'm having. – joseph Mar 16 '15 at 21:46
  • The problem might have to do with auto layout. If you have that turned on, you shouldn't be setting any frames. – rdelmar Mar 17 '15 at 02:57

1 Answers1

0

I think your problem is that you're not checking if the cell is nil.

let cellId = "cell"

var cell : CompanyTableViewCell = self.companiesTV.dequeueReusableCellWithIdentifier(cellId) as? CompanyTableViewCell
if cell == nil {
    cell = CompanyTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellId) // Your constructor here
}
Dehli
  • 5,950
  • 5
  • 29
  • 44
  • You can't use "==" with "nil". – joseph Mar 16 '15 at 20:06
  • Oops, try with the updated code (I changed a ! to a ?) – Dehli Mar 16 '15 at 20:22
  • Copied your code directly, and I still can't use "cell == nil". Also, you can't use "as?" to cast the cell-type, it won't let me do that. – joseph Mar 16 '15 at 20:27
  • Hmmm, I used the same code in one of my own projects. What's the error message? Sorry! – Dehli Mar 16 '15 at 20:28
  • First, Swift 1.2 gives me an error when I try to cast with "as?" it demands I use "as!". Secondly it gives me an error saying " Binary operator '==' cannot be applied to operands of type 'CompanyTableViewCell' and 'nil" – joseph Mar 16 '15 at 20:31