Is it possible to have differing heights in a UITableView Cell when I use several different ways of displaying the cell?

后端 未结 11 1309
一整个雨季
一整个雨季 2020-12-21 19:25

I\'ve spent several days trying to figure this out, but there doesn\'t seem to be a solution. I have a very basic UITableView cell with two labels in it. One of them will be

11条回答
  •  抹茶落季
    2020-12-21 20:15

    The height of a table view cell is decided by the table based on the rowHeight property, or on the return value of optional func tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat from the table view delegate.

    The height that may be set by the programmer for the UITableViewCell through its frame property is ignored!

    Hence you need to figure out the desired height of each cell programatically. Then you need to implement optional func tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat from the table view delegate for each of the cells.

    If the size of a cell changes while the table view is displayed, you need to either:

    • reload the table with reloadData() from UITableView, or
    • update the cell with func reloadRowsAtIndexPaths(_ indexPaths: [NSIndexPath], withRowAnimation animation: UITableViewRowAnimation) from UITableView.

    Edit

    Actually since you are using constraints in you view it should work without using ``.

    Try to replace the code in if categories[parent] == "words" { .. } with:

            cell = tableView.dequeueReusableCellWithIdentifier(childWordsCellIdentifier, forIndexPath: indexPath) as UITableViewCell
    
            // Reset content
            for subview in cell.contentView.subviews {
                subview.removeFromSuperview()
            }
            cell.prepareForReuse()
    
    
            let words                   = self.page.valueForKey("words")!.allObjects as! [Word]
            let wordsInOrder            = words.sort({Int($0.order!) < Int($1.order!) })
            let word                    = wordsInOrder[indexPath.row - 1]
    
    
            let wordLabel               = UILabel(frame: CGRectZero)
            wordLabel.text              = word.valueForKey("sanskrit") as? String
            wordLabel.font              = UIFont(name: "EuphemiaUCAS-Bold", size: 16)
            wordLabel.numberOfLines     = 0
            wordLabel.translatesAutoresizingMaskIntoConstraints = false
            wordLabel.layer.borderColor = UIColor.greenColor().CGColor
            wordLabel.layer.borderWidth = 1.0
    
            let englishWordLabel           = UILabel(frame: CGRectZero)
            englishWordLabel.text          = "Foobar don't want to play my game with my anymore."
            englishWordLabel.font          = UIFont(name: "STHeitiTC-Light", size: 16)
            englishWordLabel.numberOfLines = 0
    
            let stackView = UIStackView()
            stackView.axis = .Horizontal
            stackView.distribution = .FillProportionally
            stackView.alignment = .Fill  // EDIT
            stackView.spacing = 15
            stackView.layoutMargins = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
            stackView.layoutMarginsRelativeArrangement = true
    
            stackView.addArrangedSubview(wordLabel)
            stackView.addArrangedSubview(englishWordLabel)
    
            stackView.translatesAutoresizingMaskIntoConstraints = false
    
            cell.contentView.addSubview(stackView)
            cell.contentView.leadingAnchor.constraintEqualToAnchor(stackView.leadingAnchor).active = true
            cell.contentView.topAnchor.constraintEqualToAnchor(stackView.topAnchor).active = true
            cell.contentView.trailingAnchor.constraintEqualToAnchor(stackView.trailingAnchor).active = true
            cell.contentView.bottomAnchor.constraintEqualToAnchor(stackView.bottomAnchor).active = true
    
            cell.contentView.layoutIfNeeded()
    

    Also I would have used two different cells classes for this task rather than removing the views in the contentView.

    Please let me know how it goes!

提交回复
热议问题