使用字母标题在纯UITableview中实现搜索

时间:2013-04-26 10:00:23

标签: iphone uitableview

我正在尝试按字母顺序实现标题并在其上实现搜索。但不显示所有数据,仅显示一个字母的标题并仅显示其值。并且在搜索中也不显示除一个字母之外的值。以下是我使用的代码。

-(void)getData
{
NSLog(@"get data:%@", self.mGetDataDict);

NSLog(@"count:%d", [self.mGetDataDict count]);

NSString *getStatus = [self.mGetDataDict valueForKey:@"status"];
NSArray *dictAllKeys = [[NSArray alloc]init];
NSMutableArray *getAllDict = [[NSMutableArray alloc] init];

NSLog(@"getfinalStatus----------%@",getStatus);

if([getStatus  isEqual:[NSNumber numberWithBool:0]])
{
    NSLog(@"status:%@", getStatus);
}
else
{
    dictAllKeys = [self.mGetDataDict allKeys];
    for(int i=1; i<=[dictAllKeys count]; i++)
    {
        getAllDict = [self.mGetDataDict valueForKey:[NSString stringWithFormat:@"%d",i]];
        [self.mArrName addObject:[getAllDict valueForKey:@"name"]];
        [self.mArrPImg addObject:[getAllDict valueForKey:@"profileImage"]];
        if([seatMapStatus isEqualToString:@"yes"])
        {
        [self.mArrIng addObject:[getAllDict valueForKey:@"ing"]];
        }

        stateIndex = [[NSMutableArray alloc] init];

        for (int i=0; i<[self.mArrName count]-1; i++){
            //---get the first char of each state---
            char alphabet = [[self.mArrName objectAtIndex:i] characterAtIndex:0];
            NSString *uniChar = [NSString stringWithFormat:@"%c", alphabet];

            //---add each letter to the index array---
            if (![stateIndex containsObject:uniChar])
            {
                [stateIndex addObject:uniChar];
            }
        }
    }
}
 NSLog(@"index:%@", stateIndex);
NSLog(@"name:%@", self.mArrName);

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath     *)indexPath
{
return 60;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return [stateIndex count];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"copy arr:%d", [copyArr count]);
if(searching)
{
    NSString *alphabet = [stateIndex objectAtIndex:section];

//---get all states beginning with the letter---
NSPredicate *predicate =
[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
NSArray *states = [copyArr filteredArrayUsingPredicate:predicate];

//---return the number of states beginning with the letter---
return [states count];
   // return [copyArr count];
}
else{
    NSString *alphabet = [stateIndex objectAtIndex:section];

//---get all states beginning with the letter---
NSPredicate *predicate =
[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
NSArray *states = [self.mArrName filteredArrayUsingPredicate:predicate];

//---return the number of states beginning with the letter---
return [states count];

}
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section        {

return [stateIndex objectAtIndex:section];
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString*)title atIndex:(NSInteger)index
{
return index;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell;
cell = [self.mEventMemTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
else{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;

NSString *imageUrlString = [self.mArrPImg objectAtIndex:indexPath.row];

asyncImageView = [[AsyncImageView alloc]initWithFrame:CGRectMake(10, 5, 50, 45)];
[asyncImageView loadImageFromURL:[NSURL URLWithString:imageUrlString]];
[cell.contentView addSubview:asyncImageView];

UILabel *lblName = [[UILabel alloc]initWithFrame:CGRectMake(80, 15, 150, 30)];
if([copyArr count] >0)
{
    //---get the letter in the current section---
    NSString *alphabet = [stateIndex objectAtIndex:indexPath.section];

    //---get all states beginning with the letter---
    NSPredicate *predicate =
    [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
    NSArray *states = [copyArr filteredArrayUsingPredicate:predicate];

    if ([states count]>0) {
        //---extract the relevant state from the states object---
        //NSString *cellValue = [states objectAtIndex:indexPath.row];
       // cell.textLabel.text = cellValue;

    lblName.text = [states objectAtIndex:indexPath.row];
}
}
else{
    //---get the letter in the current section---
    NSString *alphabet = [stateIndex objectAtIndex:indexPath.section];

    //---get all states beginning with the letter---
    NSPredicate *predicate =
    [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];

    NSArray *states = [self.mArrName filteredArrayUsingPredicate:predicate];

    if ([states count]>0) {
        //---extract the relevant state from the states object---
        //NSString *cellValue = [states objectAtIndex:indexPath.row];
       // cell.textLabel.text = cellValue;
    lblName.text = [states objectAtIndex:indexPath.row];
}
}
[cell addSubview:lblName];

return cell;
}

- (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar {

searching = YES;
}

- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText {

//Remove all objects first.
[copyArr removeAllObjects];

if([searchText length]) {

    searching = YES;
    //letUserSelectRow = YES;
    //self.tableView.scrollEnabled = YES;
    [self searchTableView];
}
else {
    searching = NO;
}
[self.mEventMemTableView reloadData];
}

- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar {
[self.mSearchBar resignFirstResponder];
[self searchTableView];
}

- (void) searchTableView {

NSString *searchText = self.mSearchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];

for (NSString *sTemp in self.mArrName)
{
    NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];

    if (titleResultsRange.length)
        [copyArr addObject:sTemp];
}
searchArray = nil;

}

请指导以上内容。提前谢谢。

1 个答案:

答案 0 :(得分:0)

http://developer.apple.com/library/ios/#samplecode/TableViewSuite/Listings/3_SimpleIndexedTableView_Classes_RootViewController_m.html

UILocalizedIndexedCollat​​ion class

configureSections方法

这对你来说是一个参考,深入研究它,一切都会干净整洁:)

首先执行部分标题(搜索之前)