Reputation: 2007
Aplogies, but this is another newbie iPhone/Objective-C question.
I'm working on my first iPhone app which is a simple RPN calculator. I have a table displaying the contents of the stack, and now I'd like to fine tune the display a little bit.
I would like for the last entry in the stack to display at the bottom of the table, with the text right aligned. The right alignment was easy with
cell.textLabel.textAlignment = UITextAlignmentRight;
and I can make the table peg the last entry to the bottom of the view with this code:
NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:(stackSize - 1) inSection:0];
[tableView scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
which works great but only when there are more entries on the stack than will fit in the view. When the stack has few entries, they're still aligned to the top of the view.
I've played a bit with altering the height of the cell for the first stack entry such that the first cell fills the whole view and shrinks as new cells are added until there are enough stack entries to fill the view, at which point the cell height is left alone. This seemed promising, but I'm having some trouble getting the "big" cell to bottom align the label. (It's vertical center aligned, by default.)
As I was hacking away at the bottom alignment thing, I began to wonder if I'm making this more complicated than it needs to be. Any thoughts?
Upvotes: 0
Views: 1315
Reputation: 10080
Another approach is to resize the table view as you add or remove rows. You update the height and move the table view accordingly. Something like this looks quite OK:
- (void)resizeTableView {
CGFloat tableViewHeight = [self tableView:self.tableView numberOfRowsInSection:0] * self.tableView.rowHeight;
// remember to check the height !!
CGRect frame = self.tableView.frame;
frame.size.height = tableViewHeight;
frame.origin.y = BOTTOM_OF_VIEW - tableViewHeight;
[UIView animateWithDuration:0.3 animations:^{
[self.tableView setFrame:frame];
}];
}
If you are using the default animations for insert and delete, remember to set them to UITableViewRowAnimationNone
, otherwise it looks strange.
Upvotes: 0
Reputation: 185962
I don't know if this is kosher, but you can use view transforms to rotate the table 180° and then rotate each cell 180°. I use this trick to create sideways tables (90° instead of 180°) on an app I'm writing.
Upvotes: 3