简体   繁体   中英

Can I stop a UITableViewCell from autoresizing its subviews?

I have a UITableView with custom background images in each cell. The bottom cell has a taller image that the rest, because it contains a shadow. That means I have to extend the height of the last cell in a section to be taller than normal.

When I do this, the contents of the UITableViewCell look like they are resting at the bottom of the cell instead of the middle (because the visual cell is shorter than the actual cell). Is there a way that I can position my content from the top instead of the center, and not have the UITableViewCell resize it when a cell becomes the bottom cell (due to deleting or whatever)?

Setting the autoresizing mask on the labels to have a static top boundary doesn't work, nor does turning off autoresizesSubviews on the cell.

I also thought about simply making all the cells the same height and turning off clipsToBounds on the background image, except the background image is a stretchable image both horizontally and vertically, and needs to be able to resize based on the height of the contents of the cell.

Any other ideas that don't involve a hackish method?

I was having a similar problem ( see this question ). The solution was to override layoutSubviews in my custom table view cell class and do nothing .

Try tableView:heightForRowAtIndexPath: inside your tableViewController. Remember to include the UITableViewDelegate as a protocol.

Check out Apple iPhone Reference Library: tableView:heightForRowAtIndexPath: for limitations and what you can return.

Edit: Okay, knowing more about your question try

-(BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath{
return NO;
}

The bottom cell in a table view is just clipped visually, technically not resized since the frame value is the same internally.

I think you mean that you want your text to be vertically aligned with the top of the cell, right?

If that's it, you can achieve this by resizing the UILabel containing your text to have the same vertical size of the included text, and making sure the label is flush against the top of the cell - or as close as you like (it looks better with a few pixels' worth of padding).

How do you know what size the text is? You can use the handy-dandy sizeWithFont:... method on NSString (technically it's not in the original class definition, but added later by UIKit ). Here is some sample code:

NSString *myString = @"lorem ipsum dolor yadda";
UIFont *myFont = [UIFont fontWithName:@"Helvetica" size:14];
CGSize myStringSize = [myString sizeWithFont:myFont 
                           constrainedToSize:textLabel.frame.size
                               lineBreakMode:textLabel.lineBreakMode];

textLabel.frame = CGRectMake(10, 10, /* Or whichever origin you want. */
                             textLabel.frame.size.width, myStringSize.height);

I haven't found an automatic solution for this one. I have instead overridden several functions ( textLabel , detailTextLabel , setAccessoryView: ) to use my own labels that are autoresized how I want them to be.

I also had to override the willTransitionToState: method and delve into the implementation specifics to get the add/delete and delete confirmation buttons to move where I want them to.

This is an old post but I ran into it searching for the same problem.

Overriding layoutSubviews may hide the problem but it doesn't fix it nor work in every situation.

My guess is that several here have added labels to their custom cells using the same names. This causes the behavior that position is incorrect until scrolled as there's a name conflict going on. Here's where I found this. dequeued UITableViewCell has incorrect layout until scroll (using autolayout)

Redoing my IBOutlets with different names than the standard labels fixed it for me.

In my case I had a UIImageView only in the cell and it would resize it to the full cell size no matter what. Tried all the solutions mentioned here. What is weird is that I have another more complex cell, where I have a UIImageView+UIPageControl+UILabel+UIButton.. It only seems to behave badly when there is a single UIImageView.

My workaround was to place a UIButton instead of an image, and have the image as the background image of the button.

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