0

I have a UITableView that's top edge is attached to the bottom of a UIButton. The UITableView's edges are attached to the edges of the screen.

The UITableView has a height constraint that is initially set to 0, and then when pressing the UIButton I animate the height constraint constant to the desired size, effectively expanding the UITableView out below the UIButton. Tapping the UIButton again animates the constant back to 0 collapsing and hiding the UITableView again.

The issue I'm having is that on first load/expansion of the UITableView, as the cells appear, they do not seem to be the correct width - the right hand end (including its content) seem to expand out slightly. If I collapse the UITableView and then re-expand it again, the bug doesn't occur so it only seems to happen on first load of the cells.

I have tried this with both default Value1 type cells, and also with custom cells from Nibs and the problem occurs with both.

I have also tried calling cell.layoutIfNeeded() and cell.layoutSubviews() before return cell in cellForRowAtIndexPath() but neither have any effect.

The problem only occurs on iPhone 6 and later, presumably because the screen got slightly wider from this device. On iPhone 5S and older, the cells load fine first time. It almost seems as if the cells initially load at iPhone 5S and lower width, and then expand outwards on the larger devices.

The problem seems to be something to do with changing the UITableView's height constraint constant value, but I'm not sure why.

Thanks in advance for any help.

EDIT

Added cellForRowAtIndexPath() code:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell?

    if (cell == nil) {
        cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "Cell")
    }

    cell!.textLabel?.font = UIFont(name: "HelveticaNeue", size: 14)
    cell!.textLabel?.textColor = UIColor.lightGrayColor()
    cell!.textLabel?.text = self.someArray[indexPath.row]

    return cell!
}
myles
  • 1,681
  • 1
  • 15
  • 27
  • show some ur cellForRowAtIndexPath code – vaibby Feb 03 '16 at 11:05
  • @vaibby Added it above – myles Feb 03 '16 at 11:07
  • check in the storyboard - there is a height for cell setting that can be found when inspecting the tableview. If that isn't 0 (your intended initial height) then you might be getting the error there. – Damo Feb 03 '16 at 11:08
  • @Damo Do you mean the 'Row Height' setting? That's the only thing I can find that is similar to what you mean. I just tried setting it to 0, but it defaults to 1 and even still it had no effect. – myles Feb 03 '16 at 11:15

1 Answers1

0

try this

var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell?

if (cell == nil) {
    cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: ("Cell", forIndexPath: indexPath))
}
vaibby
  • 1,255
  • 1
  • 9
  • 23
  • Firstly, `TableCell` isn't even a class (at least in my project) and secondly, the cell dequeues and loads fine so this has no effect. In fact, I'm not actually sure I can set the style of the cell to `Value1` this way and so it's actually worse. – myles Feb 03 '16 at 11:14
  • Well the code doesn't even work. `reuseIdentifier` must be a `String` and `("Cell", forIndexPath: indexPath)` isn't a `String`. – myles Feb 03 '16 at 11:19
  • u mean ("Cell", forIndexPath: indexPath) isn't a String then how come it doesnt gave any error? – vaibby Feb 03 '16 at 11:20
  • 1
    It does... `Cannot convert value of type '(String, forIndexPath: NSIndexPath)' to expected argument type 'String?'` – myles Feb 03 '16 at 11:22
  • u found any answer for this? u can see this - http://stackoverflow.com/questions/34892039/animation-with-constraints-in-uitableviewcell-launches-incorrectly/34892374#34892374 – vaibby Feb 04 '16 at 14:56