简体   繁体   中英

Prototype reusable tableview cell rendering subviews poorly

This is the second project I have had this particular problem on. Earlier today I set up a tableview and prototype cell via the storyboard. I added subviews with tag numbers so i could get them from the cellforrowatindexpath delegate method. The subviews though are not in the right place though when i run the app. I use autosizing for layout and made sure to set the delegates. Has anyone had this problem?

POSSIBLY RELEVANT: Sometimes when i leave the storyboard and come back, the subviews changed their frames so that they are flat (height = 0) or x has changed to like 1,500 randomly. No clue why but this happened on the old project I had the problem with as well. The old project I resolved the issue by resetting the frames of all the subviews in the cellforrowatindexpath method but I don't like that as a legit solution.

EDIT: Here is some code and screenshots.

//MARK: - UITableview delegate
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 93.0
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 7;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("order") as? UITableViewCell
    println("cell: \(cell)")
    var cancel = cell?.viewWithTag(4) as UIButton
     var price = cell?.viewWithTag(3) as UILabel
     var date = cell?.viewWithTag(2) as UILabel
     var address = cell?.viewWithTag(1) as UILabel
    cancel.addTarget(self, action: "cancelOrder:", forControlEvents: UIControlEvents.TouchUpInside)

    return cell!
}

截图

I've had this issue recently. It seems to be a bug with autolayout for the content view in custom cells.

All I did was in the awakeFromNib for my custom cells reset the autoresizing mask and everything started working as expected.

- (void) awakeFromNib
{
    self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM