UITableView does not return to the right place after a bounce when using custom section height - iphone

I need to set custom section height for my table view, and I'm trying to do it in pretty straightforward way by calling some delegate method:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 39.0;
}
I do get desired result - a taller section but I'v noticed one problem. When I tap the table (start dragging), move my finger up to top, and release the table, then the table bounces back but does not return to the correct initial place. If What I mean is the section header overlaps the first row. Here is the screen:
Any ideas why this happens or what workarounds exist?
UPDATE: I also tried increasing section height property of the tableView in IB. This increases the height but the same problem exist.

It sounds like you're dragging the table up so that the last row of the table, plus empty space below it, is showing, like this:
When you let go, it slides the table back down so that bottom edge of the last table row is flush against the bottom edge of the view.
This is the expected behavior for a table view. You're seeing the top row slip partly underneath your section header because the view height, minus the section header height, isn't an integer multiple of the row height. If you don't like this, you need to make sure the view height minus the section header height is an integer multiple of the row height.

There is another way to set the section height without using the delegate:
self.tableView.sectionHeaderHeight = 39.0;
If this does not help, you will have to fiddle with the UIScrollView Delegate.
Also, from your screen shot I see that you might be interested in the property tableHeaderView, which is a header above the table that will scroll off-screen when the table view is scrolled.

Related

Scroll UITableView Header?

I have a custom UITableView header which I want to be able to scroll above the top of the table. Normally the header would stick to the top of the table, is it not possible to somehow change to scrolling ability so that it can go beyond that and scrolls with the cells in the table?
Thanks.
Well if that is the desired behavior you want, just put the header as the first cell's content. It is possible to customize any particular cell you want. UITableViewDelegate documentaion will help you in that matter.
PS: the whole point of using tableView header is to make it stick to the top of the window.
EDIT: If it is necessary that you have to do the way you want, then you can try this: move your tableView a little down by setting its contentOffset. eg: myTableView.contentOffset= CGPointMake(0,heightOfYourView) . Now add yourView at the top
myTableView.tableViewHeader = myCustomTableHeaderView;
This would set myCustomTableHeaderView as the header of your table view and it would scroll with the table view.
By implementing tableView:viewForHeaderInSection: for section of index 0, you can instead have the header as the first section that should disappear when it scrolls. In this way, it's not a header for the whole UITableView.
If you only have one header for the whole table, can't you just set the tableHeaderView property?
You might, like me, have been searching for the tableHeaderView property. See this SO question for more info.
You should use Grouped TableView Style and in your ViewDidLoad method, add following line of code:
myTableView.backgroundColor=[UIColor clearColor];
myTableView.opaque=NO;
myTableView.backgroundView=nil;
Also in nib file, you should clear background color of grouped table;
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat sectionHeaderHeight = 40;//Change as per your table header hight
if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
} else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
}
}

iPhone UITableView Add content at the top without scrolling

I have a potentially very long scrolling log of messages within a UITableView The user would be able to navigate the table by scrolling.
What I'm trying to do is add new content at the top of the table. If the top of the table is visible, the new row would push the rest of the content down. But if the first row of the table is not visible, the table would silently add another row on top without scrolling.
I'm doing this to prevent the table scrolling to the top from interrupting the user browsing data. If I ask the table to simply scroll to the new insertion point, the user's position within the table would be lost, frustrating the user.
One of the ways I can try to implement this is by inserting data into an NSMutableArray at index 0. Would this work? An extra question: how expensive is inserting data at the beginning of an NSMutableArray. I would expect that such operation is optimized, but would like to make sure. What am I missing with this implementation?
Thank you!
How to insert without bumping the view:
Use the tableView's (UIScrollView) contentOffset values to find where the user is currently scrolled to.
Add the new row at the top, with animated:NO
Update the tableView's contentOffset to be where the user was at plus whatever the height of your row is.
How to avoid causing issues doing this while the user is dragging:
Keep track of when the user is dragging, and if you want to insert rows during a drag / motion, add them to a queue that is executed after the user releases.
CGSize beforeContentSize = self.tableView.contentSize;
[self.tableView reloadData];
CGSize afterContentSize = self.tableView.contentSize;
CGPoint afterContentOffset = self.tableView.contentOffset;
CGPoint newContentOffset = CGPointMake(afterContentOffset.x, afterContentOffset.y + afterContentSize.height - beforeContentSize.height);
self.tableView.contentOffset = newContentOffset;
Just posted an answer with code snippets here
Keep uitableview static when inserting rows at the top
Basically:
Save the scroll offset where you would have called beginUpdate:.
In place of calls to InsertCell you add the cell's height to the saved offset.
Where you would have called endUpdate:, call reloadData, then scroll by the saved offset with NO animation.

Is there a way to make the very LAST section header scrollable to the top?

Basically I have complete default behaviour going on with the section headers. I just wish that the user could scroll the LAST section header to the very top.
E.g You have a table where rows with 44 pixel height. Table has a height of ~500 pixels.
If the LAST section only has 2 entries, you will not be able to scroll this section to the very top of the table, thereby leaving the previous section header at the top and the last section header somehwere in the middle of the screen, revealing the last 2 rows of the last section.
I'd like it so the user can scroll the last section header to the top - leaving empty rows (if necessary) for the bottom half of the table if the last section contains only a few rows.
Is this at all possible?
Yes it is.........
what you have to do is just set the contentInset for the table view....
[yourTableView setContentInset:UIUIEdgeInsetsMake(0,0,yourScrollableHeight,0)];
**Update***
Here is the procedure to know the cell in last section ...(cellForRowAtIndexPath code)
int numberOfSections = [yourTableView numberOfSections];
if(it's last section... [indexPath.section == numberOfSections-1])
{
int numberOfRowsInLastSection = [yourTableView numberOfRowsInSection:indexPath.section];
//default height of row is .. 44px...
// now I hope you can calculate this......
//and here use above line of code for setting scroll behavior...
}
thanks,

UItableview returns more row?

i have done like
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
tableview is in UITableViewStylePlain.but it shows correctly 3 data on 3 tableview cell.but after that there is empty tableview cells ...but when i declared UITableViewStyleGrouped, it shows only 3 tableview cells... perfectly...what i have to do disappear empty tableview cell
in UITableViewStylePlain..any help pls?
Your table contains 3 table cells and those cells beneath it are not empty table cells. The row seperators are just drawn based on the previous row height giving the impression that there are more cells than you actually specified.
You could set the separatorStyle of the table view to UITableViewCellSeparatorStyleNone, either in code or in Interface Builder. But doing so disables the seperators all together, so you would have to draw some kind of separator yourself in your cells if you still wanted a grafical separation between the actual cells.
Another option would be to set the color of the separator to the color of the background of the table.
I myself would not worry about this if you're application has a standard table look, as it's default behaviour & users should be used to seeing that. Altough I must admit that I have set the separatorStyle to none in a previous project, because the table had a look that deviated from the standard table look. I did have to draw a fine separator line at the bottom in the table cell.

Best (Any) way blend tableview section header onto top of a grouped tableview

I'd like to add section headers to my grouped table view's sections but I'd like them to appear seamless (see image). The default, as we're all well aware of, is rounded top corners on the first row of a grouped table view cell so it ends up looking like crap when you merge them.
Any way to specify when indexPath.row = 0 that the UITableViewCell should use row style "middle" or something like that?
If not then what are my options? I guess I could scratch the section header and use Row 0 as a quasi-header then push my array data +1 to fill the rest of the table? I'd rather not roll my own from scratch...if possible.
Sample Table http://img35.imageshack.us/img35/8181/sampletable.png
Edit:
"Crap" looks like this:
alt text http://img25.imageshack.us/img25/9748/crapsection.png
Don't do what you're doing, it's against HIG
Ok, ok, I'll tell you how to do it:
You're going to want to do your own cell background views. The default grouped one is not what you want.
When a tableview asks you for a cell, set its backgroundView and selectedBackgroundView to something that looks appropriate for its place in the tableview.
Usually, this means a UIImageView with the appropriate image, though you can go wild here with a custom view, but there are gotchas.
So in your case, you would do
if (indexPath.row > sectionRowCount - 1) {
//Not the last row
//Put in the middle background
} else {
//Put in the end background
}
Then you'll want a custom table section header, but that's pretty easy.
In your case, you probably won't have to worry about when there's just one row, so that makes things even easier.
Take a look at the tutorial here:
cocoa with love basically what you need is 3 different images. One for the top row, one for the bottom, and a 3rd for the middle rows.
You could also not use the section header, but instead use a custom cell as the first cell of the section. So when ([indexPath row] == 0), return a custom cell that is the "header" and then return the "regular" cells (offset by one row) for the rest. You'll also have to make adjustments to the numberOfRowsInSection function to return +1.