Welcome, Guest
Username Password: Remember me

Position of delegate callback in searchBarCancelButtonClicked:
(1 viewing) (1) Guest
  • Page:
  • 1

TOPIC: Position of delegate callback in searchBarCancelButtonClicked:

Position of delegate callback in searchBarCancelButtonClicked: 1 year, 5 months ago #1

So in - (void)searchBarCancelButtonClicked:(UISearchBar *)sBar, I need to be able to hide the cancel button after the user has tapped cancel. I also need to reset the index titles on the right so they show again. I previously hid them by setting them to nil in searchBarShouldBeginEditing (which I added to SCTableViewModel). However, in order to be able to show the index titles again, you need to tell the table view to reloadData. This is already being done in searchBarCancelButtonClicked. But it's doing it before the callback to the delegate method. Unfortunately this means that I also must tell the table view to reloadData. This causes a delay since when you tap the cancel button, you are effectively reloading the table twice.

Can the delegate callback method be put at the beginning of searchBarCancelButtonClicked instead of the end?

So I would like to see:

 
- (void)searchBarCancelButtonClicked:(UISearchBar *)sBar
{
 
if([self.delegate conformsToProtocol:@protocol(SCTableViewModelDelegate)]
&& [self.delegate respondsToSelector:@selector(tableViewModelSearchBarCancelButtonClicked:)])
{
[self.delegate tableViewModelSearchBarCancelButtonClicked:self];
}
 
[self.searchBar resignFirstResponder];
self.searchBar.text = nil;
 
[filteredArray release];
filteredArray = nil;
[self generateSections];
[self.modeledTableView reloadData];
 
 
}
 


Instead of:
 
- (void)searchBarCancelButtonClicked:(UISearchBar *)sBar
{
[self.searchBar resignFirstResponder];
self.searchBar.text = nil;
 
[filteredArray release];
filteredArray = nil;
[self generateSections];
[self.modeledTableView reloadData];
 
if([self.delegate conformsToProtocol:@protocol(SCTableViewModelDelegate)]
&& [self.delegate respondsToSelector:@selector(tableViewModelSearchBarCancelButtonClicked:)])
{
[self.delegate tableViewModelSearchBarCancelButtonClicked:self];
}
}
 



Thanks!

Brendan
  • tapforms
  • OFFLINE
  • Expert Boarder
  • Posts: 120
  • Karma: 4
Thanks!
Brendan
www.tapforms.com

Re: Position of delegate callback in searchBarCancelButtonClicked: 1 year, 5 months ago #2

Great feedback as usual Brendan, thank you very much!

You're right, I think it's a much better design to have the reloadData call after the delegate method call. We will refactor the method to look like this though:

 
- (void)searchBarCancelButtonClicked:(UISearchBar *)sBar
{
[self.searchBar resignFirstResponder];
self.searchBar.text = nil;
 
[filteredArray release];
filteredArray = nil;
[self generateSections];
 
if([self.delegate conformsToProtocol:@protocol(SCTableViewModelDelegate)]
&& [self.delegate respondsToSelector:@selector(tableViewModelSearchBarCancelButtonClicked:)])
{
[self.delegate tableViewModelSearchBarCancelButtonClicked:self];
}
 
[self.modeledTableView reloadData];
}
 


Thanks again!
  • tarekskr
  • OFFLINE
  • Administrator
  • Posts: 2404
  • Karma: 72
  • Page:
  • 1
Time to create page: 0.88 seconds