Posted By

sag333ar on 11/17/10


Tagged

UITableView


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

sag333ar

Tintenklecks
cnathan


SearchResultTableViewController Snippet


 / Published in: Objective C
 

  1. #pragma mark -
  2. #pragma mark Table view data source
  3.  
  4. // Customize the number of sections in the table view.
  5. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  6. return 1;
  7. }
  8.  
  9.  
  10. // Customize the number of rows in the table view.
  11. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  12. return (tableView==tblMain)?[self.arFoodData count]:[self.arFilteredFoodData count];
  13. }
  14.  
  15. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  16. return 141.0f;
  17. }
  18.  
  19. #define kTagForCellView 1
  20.  
  21. // Customize the appearance of table view cells.
  22. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  23.  
  24. static NSString *CellIdentifier = @"Cell";
  25.  
  26. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  27. if (cell == nil) {
  28. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
  29. [cell setBackgroundColor:[UIColor clearColor]];
  30. [cell setAutoresizesSubviews:NO];
  31. [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
  32. }
  33.  
  34. UIView *v=[cell viewWithTag:kTagForCellView];
  35. if(v) { [v removeFromSuperview]; v = nil; }
  36.  
  37.  
  38. NSArray *arr=(tableView==tblMain)?arFoodData:arFilteredFoodData;
  39.  
  40.  
  41. CCellView *c=[[arr objectAtIndex:indexPath.row] valueForKey:@"CCellView"];
  42. if(c.view) [c viewDidLoad];
  43. c.view.tag=kTagForCellView;
  44. [cell addSubview:c.view];
  45.  
  46. return cell;
  47. }
  48.  
  49.  
  50. - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
  51. [self filterContentForSearchText:searchString];
  52. return YES;
  53. }
  54.  
  55. - (void)filterContentForSearchText:(NSString*)searchText {
  56. [arFilteredFoodData removeAllObjects];
  57. for (NSDictionary *d in arFoodData) {
  58. NSRange r=[[d valueForKey:@"title"] rangeOfString:searchText options:NSCaseInsensitiveSearch];
  59. if(r.length>0)[arFilteredFoodData addObject:d];
  60. }
  61. }

Report this snippet  

You need to login to post a comment.