views:

1386

answers:

4

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?

A: 

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);
Tyler
My problem is not sizing it; it's making sure it stays that size. The label is the standard UITableViewCell.textLabel, and no matter where I position it, it seems to be moving back to its own location any time the Cell's transform changes (i.e. turning edit mode on/off)
Ed Marty
+1  A: 

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;
}
David Wong
Thanks, but no thanks. I'm already using that to return the height of the row (it has to be BIGGER for the bottom row, because the image is bigger). If I return the same size for all rows, the bottom row looks too small.
Ed Marty
You can select a row using the IndexPath to only resize the last row only and leave the rest of your rows resizing correctly. An if(indexPath.row == [array count]) return 100 else return 50; might solve your problem of the cell auto-resizing.
David Wong
No, no, no. I AM ALREADY doing this, because the last row needs to be larger than the others, but VISUALLY it needs to look the same (hence I am using a stretchable image). The problem of autoresizing isn't that the cell itself changes height, but that it moves its subviews around, when moving in and out of edit mode, for example.
Ed Marty
Oh, now I know what you're doing. I can see that you're getting frustrated, but the question wasn't succinct. Anyway I believe you're looking for "tableView:shouldIndentWhileEditingRowAtIndexPath:" where you return a boolean for the row you don't wish to indent while editing.
David Wong
A: 

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.

Ed Marty
+2  A: 

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.

Tim Keating
THANK you! Finally, a real answer!
Ed Marty