UITableView Header Prevention

One of the screens of my iPhone application is the UITableView, which has a search bar in the table header. I want the search bar to be hidden until the user pulls it, so I use this line in the viewDidLoad method:

[self.tableView setContentOffset:CGPointMake(0,40) animated:NO]; 

This displays the tableView correctly with the title scrollable at the top of the screen.

My problem occurs when I try to delete a row using this method:

 [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:myIndexPath] withRowAnimation:UITableViewRowAnimationRight]; 

When this line is executed, it correctly shifts the line on the right side of the screen. If the table contains only a few rows, then the table title (with my search bar) scrolls down to fill the space freed up by the new deleted row. I do not want this to happen. I want the title to remain hidden until the user scrolls with his finger.

If the tableView has enough rows to fill the window, the table is filled at the bottom, and the title remains hidden. The problem arises only when the screen is not filled with rows.

Any idea on how I can make UITableView not display the table title when deleting a row?


Application: I found that the same thing happens when I put the UITableViewController in edit mode with the following code:

 [self setEditing:NO animated:YES]; 

It goes into edit mode, displays red β€œdelete row” circles and displays the table title. Maybe this is something with the animation.

+4
source share
4 answers

This was published some time ago, but I have come across the same issue recently. Repeating setContentOffset after deleteRowsAtIndexPaths solves it.

+1
source

Are your rows fixed in height? Perhaps you could determine the number of lines remaining and reset the offset when deleting.

0
source

Try listening to the call to UIScrollViewDelegate - (void)scrollViewDidScroll:(UIScrollView *)scrollView on your tableView. If the contentOffset becomes less than 40 when you don't want it, stop scrolling.

0
source

The only GOOD solution I found is this: you add a footer view to the table with an initial height of 1 pixel (assign a UIView frame (0,0,320,1)).

Each time your data changes or the height of the view changes (use the UIViewController viewWillLayoutSubviews method for iOs> = 5.0), you call the update_footer method. In this method, you calculate the number of sections and the number of rows, then you calculate the height of the data by multiplying them by the height of the line and the height of the separator. Now the height of the footer is the height of the table, minus the estimated height of the data (then if it is <1 pixel, set it to 1).

Set the footer (0,0,320, this height) and remember to assign the footer view again to the table footer property (table.FooterView = table.FooterView :)) to tell the table that the footer height is changing.

I'm sorry that I do not give the code, now I use the studio Xamarin (formerly monophonic) and write in C #.

By the way, this method could be used if you need custom images of cell separators, including between non-existing cells. :)

0
source

All Articles