UITableViewCell, dynamically changing height from the dynamic size of UIView inside it



  • I'll tell you what to use. autolayoutUITableViewController and viewWillAppear(bool) the following:

        self.tableView.rowHeight = UITableViewAutomaticDimension
        self.tableView.estimatedRowHeight = 1000
    

    The point is that in the cell the View is to be stretched, the dimensions of which are defined in cellForRowAtIndexPathI mean,

    cell = tableView.dequeueReusableCellWithIdentifier("TagViewCell", 
    forIndexPath: indexPath) as! TagViewCell
    cell.configure(product.tags)
    

    configure receives a set of lines and fills it with basics. UIViewwhich I call TagHolder (in fact just empty UIView) other one-time view I called TagView♪ On the click, the laser will be able to obtain the necessary information and pass the pages. Logic is that tends to keep the currents out of line, like it might look like that.

    введите сюда описание изображения

    three lines, or if they are not enough, one, and therefore the height TagHolder'а Changes and changes after the cell is painted. awakeFromNiband its size UITableView remains as painted MainStoryboard

    Is there a way to configurate? TagHolder before iOS calculate the size of the cell?

    UPDATE:

    Autolayout I use precisely because on the page, several different cells and some of them contain dynamic view, e.g. product decription cell contains UITextView, the value of which is different for each product, but is configured after

     dequeueReusableCellWithIdentifier
    

    So I'm probably wrong with dynamic change TagHolder'a.

    class TagHolder: UIView {
    
    override var width: CGFloat {
        didSet {
            reload()
        }
    }
    
    var stringTags : [Tag] = []
    var tags : [TagView] = []
    
    var lines : CGFloat = 1 {
        willSet {
            let tag = TagView.create(0, y: 0, tag: Tag(name: "i", id: "0", id1c: "0"))
            self.height = newValue * tag.height
        }
    }
    var curx : CGFloat = 0
    var cury : CGFloat = 0 {
        willSet {
            if newValue > cury { lines += 1 }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
    }
    
    func reload() {
        let stags = stringTags
        stringTags = []
        tags = []
        for tag in self.tags {
            tag.removeFromSuperview()
        }
        self.add(stags)
    }
    
    @nonobjc func add(tags : [Tag]) {
        self.stringTags += tags
        for tagstring in tags {
            var tag = TagView.create(curx, y: cury, tag : tagstring)
    
            let inline = curx + tag.width <= screenWidth
            curx = inline ? curx + tag.width : 0
            cury = inline ? cury : cury + tag.height
            tag = inline ? tag : TagView.create(curx, y: cury, tag : tagstring)
            self.addSubview(tag)
            self.tags.append(tag)
            curx = inline ? curx : curx + tag.width
        }
    }
    
    @nonobjc func add(tag : Tag) {
        let a : [Tag] = [tag]
        self.add(a)
    }
    

    }

    self.height defined in extension for UIView

    var height:CGFloat {
    get {
    return self.frame.size.height
    }
    set {
    self.frame.size.height = newValue
    }
    }



  • If such cells are not large, you can create them with pens (in viewDidLoad, for example), fill TagHolder and transmit it to the delegates ' and cell ' s methods and size.

    You can change the cell method.configure so he can only calculate the size. All right, in the height of ForRowAtIndexPath, you return the size, in the cell ForRowAtIndexPath, you fill the cell.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2