Preventing Display of UITableView header
One of the screens in my iPhone app is a UITableView that has a search bar in the table header. I want the search bar to be hidden until the user pulls it down, so I use this line in my viewDidLoad method:
[self.tableView setContentOffset:CGPointMake(0,40) animated:NO];
This correctly displays the tableView with the header scrolled off the top of the screen.
My problem occurs when I try to delete a row using this method:
[self.tableView deleteRowsAtIndexPaths:[NSArray arra开发者_运维技巧yWithObject:myIndexPath] withRowAnimation:UITableViewRowAnimationRight];
When that line executes, it correctly slides the row off the right side of the screen. If the table only contains a few rows, though, the table header (with my search bar) is scrolled down to fill up the space freed up by the newly deleted row. I don't want this to happen. I want the header to remain hidden until a user scrolls it down with his finger.
If the tableView has enough lines to fill the window, the table fills up from the bottom and the header remains hidden. The problem only occurs when the screen is not completely filled with rows.
Any idea on how I can force the UITableView to not display the table header when I delete a row?
Addendum: I discovered that the same thing happens when I put the UITableViewController into editing mode using code like this:
[self setEditing:NO animated:YES];
It goes into editing mode, displays the red "delete row" circles, and displays the table header. Maybe it's something with the animations.
This was posted a while ago but I ran into the same issue recently. Repeating setContentOffset after deleteRowsAtIndexPaths solves it.
Are your rows a fixed height? Perhaps you could detect the number of remaining rows, and reset the offset upon deletion.
Try listening with the UIScrollViewDelegate
call - (void)scrollViewDidScroll:(UIScrollView *)scrollView
on your tableView. If the contentOffset becomes less than 40 when you don't want it to be, stop the scrolling.
The only GOOD solution I have found is the following: You add the footer view to the table with initial height of 1 pixel (alloc UIView with (0,0,320,1) frame).
Every time your data changes, or the view height changes (use UIViewController's viewWillLayoutSubviews method for iOs >=5.0) you call the update_footer method. In this method, you calculate number of sections and number of rows, then you calc the height of the data by multiplying those to row height and section separator height. Now the footer height is tableview height minus that calculated data height (then if it is < 1 pixel, set it to 1).
Set the footer's frame to (0,0,320,that height), and do not forget to assign the footer view again to the table's footer property (table.FooterView = table.FooterView :)) to tell table that the footer's height is changed.
I'm sorry that I give no code, I use Xamarin studio (former mono touch) now and write in C#.
By the way, this method could be used if you want custom cell separator images, including those between non-existant cells. :)
精彩评论