UITableCell does not resize

While working on my first iPhone app, I ran into a problem when trying to use "auto resize a table cell. When I load the app, the first few cells will always be huge, about 6 times their size. Then when I scroll down and create a backup, they will be reset to the height they should be. (see image for example, hard to explain)

http://imgur.com/a/yzd6P (first 2 images)

So far I have tried several stackoverflow guides and answers that I could not post here because I can only post 2 links, most of them just involved adding the UITableViewAutomaticDimension attribute.

Ive made a cell in the storyboard (contains 4 c # labels with strings set to 0 and 3 images (see third image)) and gave it an id and then created a custom class to change the text of the labels.

Resize the cell as follows:

self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 104

      

Using the following line seems to fix the problem on most devices, but still continues to exist on iPhone 6 plus (fourth image):

tableView.reloadSections(NSIndexSet(indexesInRange: NSMakeRange(0, self.tableView.numberOfSections())), withRowAnimation: UITableViewRowAnimation.Fade)

      

Drawing a cell:

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

    if cell == nil {

        cell = TableCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "tableCell")
    }

    cell?.lblObituaryName.text = obituaryArray[indexPath.row].name
    cell?.lblObituaryBirth.text = obituaryArray[indexPath.row].dateOfBirth
    cell?.lblObituaryDeath.text = obituaryArray[indexPath.row].dateOfDeath
    cell?.lblObituaryPlace.text = obituaryArray[indexPath.row].city

    cell?.setNeedsUpdateConstraints()
    cell?.updateConstraintsIfNeeded()


    return cell!
}

      

The cell setup in the storyboard is the fifth and final image.

Cellular Limitations:

Table cell is 104.0 high

Main title label:
Trailing space to: Superview = 0
Leading space to: Superview = 0
Top space to: Superview = 0
Bottom space to: Label1 = 6
Bottom space to: Label1Image = 1

Label 1:
Trailing space to: Superview = 0
Leading space to: Label1Image = 5
Bottom space to: Label2 = 6
Top space to: Main Label = 6

Label 1 Image:
Leading space to: Superview = 0
Width = 25
Height = 25
Trailing space to: Label1 = 5
Bottom space to: Label2Image = -3
Top space to: Main Label = 1

Label 2:
Trailing space to: Superview = 0
Leading space to: Label2Image = 5
Bottom space to: Label3 = 6
Top space to: Label2 = 6

Label 2 Image:
Leading space to: Superview = 0
Width = 25
Height = 25
Trailing space to: Label2 = 5
Bottom space to: Label3Image = -3
Top space to: Label1Image = -3

Label 3:
Trailing space to: Superview = 0
Bottom space to: Superview = -1
Leading space to: Label3Image = 5
Top space to: Label2 = 6

Label 3 Image:
Leading space to: Superview = 0
Width = 25
Height = 25
Trailing space to: Label3 = 5
Top space to: Label2Image = -3

      

Fixed bug below by changing the priorities of the constraints to 999. However, search for the answer to the question above.

Unable to simultaneously satisfy constraints.

Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 

(

    "<NSLayoutConstraint:0x7a0affb0 UILabel:0x7a079ea0'Mw. J.H. Zwierenga'.top == UITableViewCellContentView:0x7a074170.topMargin>",

    "<NSLayoutConstraint:0x7a0b00a0 V:[UILabel:0x7a079ea0'Mw. J.H. Zwierenga']-(6)-[UILabel:0x7a0394f0'21 May 2015']>",

    "<NSLayoutConstraint:0x7a0b0190 V:[UILabel:0x7a0394f0'21 May 2015']-(6)-[UILabel:0x7a0a1f20'1 December 1935']>",

    "<NSLayoutConstraint:0x7a0b0280 UITableViewCellContentView:0x7a074170.bottomMargin == UILabel:0x7a094e10'Oudeschild'.bottom - 1>",

    "<NSLayoutConstraint:0x7a0b02b0 V:[UILabel:0x7a0a1f20'1 December 1935']-(6)-[UILabel:0x7a094e10'Oudeschild']>",

    "<NSLayoutConstraint:0x7a0b2fe0 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7a074170(0)]>"

)



Will attempt to recover by breaking constraint 

<NSLayoutConstraint:0x7a0b02b0 V:[UILabel:0x7a0a1f20'1 December 1935']-(6)-[UILabel:0x7a094e10'Oudeschild']>



Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.

The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

      

Let me know if more information is needed.

+3


source to share


2 answers


  • Use layoutIfNeeded immediately to limit the update.
  • For error warning you must set one of your priorities with a vertical limit to 999.


Everything works correctly if the "Accessory" parameter is set to "No"

+1


source


try it



override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
        return 104;
}

      

0


source







All Articles