Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If I understood correctly then what you wish to do to get categories from database &amp; display it on a tableView with alphabetical sorting, index on right &amp; search bar on top. Ideally, you would like to display the Contacts application kind of a view. If that's correct, use below code for fetching items from DB &amp; rebuilding (or resetting) it - </p> <pre><code>const char *sql = "select cid, category from Categories ORDER BY category DESC"; sqlite3_stmt *statementTMP; NSMutableArray *arrayTmp = [[NSMutableArray alloc] init]; int error_code = sqlite3_prepare_v2(database, sql, -1, &amp;statementTMP, NULL); if(error_code == SQLITE_OK) { while(sqlite3_step(statementTMP) == SQLITE_ROW) { int cid = sqlite3_column_int(statementTMP, 0); NSString *category = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statementTMP, 1)]; NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; [dict setObject:category forKey:@"Category"]; [dict setObject:[NSNumber numberWithInt:cid] forKey:@"CID"]; [arrayTmp addObject:dict]; [dict release]; [category release]; } } sqlite3_finalize(statementTMP); sqlite3_close(database); self.allCategories = arrayTmp; [arrayTmp release]; </code></pre> <p>And then rebuild the items using this function - </p> <pre><code>- (void)rebuildItems { NSMutableDictionary *map = [NSMutableDictionary dictionary]; for (int i = 0; i &lt; allCategories.count; i++) { NSString *name = [[allCategories objectAtIndex:i] objectForKey:@"Category"]; NSString *letter = [name substringToIndex:1]; letter = [letter uppercaseString]; if (isdigit([letter characterAtIndex:0])) letter = @"#"; NSMutableArray *section = [map objectForKey:letter]; if (!section) { section = [NSMutableArray array]; [map setObject:section forKey:letter]; } [section addObject:[allCategories objectAtIndex:i]]; } [_items release]; _items = [[NSMutableArray alloc] init]; [_sections release]; _sections = [[NSMutableArray alloc] init]; NSArray* letters = [map.allKeys sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]; for (NSString* letter in letters) { NSArray* items = [map objectForKey:letter]; [_sections addObject:letter]; [_items addObject:items]; } } </code></pre> <p>Now, displaying items in tableView, use below methods -</p> <pre><code>#pragma mark - #pragma mark Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView { if (_sections.count) return _sections.count; else return 1; } - (NSInteger)tableView:(UITableView*)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { if (tableView.tableHeaderView) { if (index == 0) { [tableView scrollRectToVisible:tableView.tableHeaderView.bounds animated:NO]; return -1; } } NSString* letter = [title substringToIndex:1]; NSInteger sectionCount = [tableView numberOfSections]; for (NSInteger i = 0; i &lt; sectionCount; i++) { NSString* section = [tableView.dataSource tableView:tableView titleForHeaderInSection:i]; if ([section hasPrefix:letter]) { return i; } } if (index &gt;= sectionCount) { return sectionCount-1; } else { return index; } } - (NSArray*)lettersForSectionsWithSearch:(BOOL)withSearch withCount:(BOOL)withCount { if (isSearching) return nil; if (_sections.count) { NSMutableArray* titles = [NSMutableArray array]; if (withSearch) { [titles addObject:UITableViewIndexSearch]; } for (NSString* label in _sections) { if (label.length) { NSString* letter = [label substringToIndex:1]; [titles addObject:letter]; } } if (withCount) { [titles addObject:@"#"]; } return titles; } else { return nil; } } - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return [self lettersForSectionsWithSearch:YES withCount:NO]; } - (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section { if (_sections.count) { NSArray* items = [_items objectAtIndex:section]; return items.count; } else { return _items.count; } } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { if (_sections.count) return [_sections objectAtIndex:section]; return nil; } - (id)tableView:(UITableView *)tableView objectForRowAtIndexPath:(NSIndexPath *)indexPath { if (_sections.count) { NSArray *section = [_items objectAtIndex:indexPath.section]; return [section objectAtIndex:indexPath.row]; } else { return [_items objectAtIndex:indexPath.row]; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // Create your UITableViewCell. // Configure the cell. NSDictionary *dict = [self tableView:tableView objectForRowAtIndexPath:indexPath]; cell.textLabel.text = [dict objectForKey:@"Category"]; cell.detailTextLabel.text = [NSString stringWithFormat:%d, [[dict objectForKey:@"CID"] intValue]]; return cell; } #pragma mark - #pragma mark Table view delegate - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { if (isSearching) return nil; NSString *title = @""; if (_sections.count) { title = [[_sections objectAtIndex:section] substringToIndex:1]; } else { return nil; } UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)]; view.backgroundColor = [UIColor colorWithRed:(58/255.0) green:(27/255.0) blue:(6/255.0) alpha:1.0]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 1, 50, 18)]; label.textColor = [UIColor whiteColor]; label.backgroundColor = [UIColor clearColor]; label.font = [UIFont boldSystemFontOfSize:17.0]; label.text = title; [view addSubview:label]; [label release]; return [view autorelease]; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSDictionary *dict = [self tableView:tableView objectForRowAtIndexPath:indexPath]; NSLog(@"selected row id:%d, name:%@", [dict objectForKey:@"Category"], [[dict objectForKey:@"CID"] intValue]); } </code></pre> <p>The rest part is implementing the UISearchBarDelegate and implementing searching of tableView which can be done using below code:</p> <pre><code>- (void)searchBar:(UISearchBar *)searchbar textDidChange:(NSString *)searchText { [_sections removeAllObjects]; [_items removeAllObjects]; if([searchText isEqualToString:@""] || searchText == nil) { [self rebuildItems]; return; } NSInteger counter = 0; for(NSDictionary *dict in allCategories) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSRange r = [[dict objectForKey:@"Category"] rangeOfString:searchText options:NSCaseInsensitiveSearch]; if(r.location != NSNotFound) { if(r.location == 0) { [_items addObject:dict]; } } counter++; [pool release]; } [contactList reloadData]; } </code></pre> <p>Hope this is what you're looking for.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload