码迷,mamicode.com
首页 > 其他好文 > 详细

UITableView 索引的设置

时间:2014-07-28 15:34:33      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:des   数据   io   for   cti   ar   new   app   

if (_indexCollation == nil) {

            _indexCollation = [UILocalizedIndexedCollation currentCollation];

        }

        NSArray *localTracks = [NSArray array];

        

        //查找本地音乐

        NSEntityDescription * trEty = [NSEntityDescription entityForName:@"Track" inManagedObjectContext:ShareAppDelegate.managedObjectContext];

        NSFetchRequest *frq = [[NSFetchRequest alloc]init];

        [frq setEntity:trEty];

        

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"beAdded == %@",[NSNumber numberWithBool:YES]];

        [frq setPredicate:predicate];

        

        localTracks =[ShareAppDelegate.managedObjectContext executeFetchRequest:frq error:nil];

        

        //每个索引下的数组

        NSMutableArray *newLocalTracks = [[NSMutableArray alloc] initWithCapacity:[[_indexCollation sectionTitles] count]];

        for (int i = 0; i < [[_indexCollation sectionTitles] count]; i ++) {

            NSMutableArray *array2 = [[NSMutableArray alloc] initWithCapacity:1];

            [newLocalTracks addObject:array2];

        }

        

        for (Track *track in localTracks) {

            if (![playlist__.pltracksship containsObject:track]) {

//判断这个数据应该处于本地索引的那个序列下

                NSInteger index = [_indexCollation sectionForObject:track collationStringSelector:@selector(trackTitle)];

                NSMutableArray *array = [newLocalTracks objectAtIndex:index];

                [array addObject:track];

                

                __tracksCount ++;

            }

        }

        

//处理结果:将结果中统计数为0的结果对应的索引去掉。

        for (int i = 0; i < [[_indexCollation sectionTitles] count]; i++) {

            NSMutableArray *array = [newLocalTracks objectAtIndex:i];

            

            if (array.count > 0) {

                //获得排序结果

                NSArray *sortedarray = [_indexCollation sortedArrayFromArray:array collationStringSelector:@selector(trackTitle)];

                

                //替换原来数组

                [newLocalTracks replaceObjectAtIndex:i withObject:sortedarray];

            }

        }

        

        //删选没有内容的array 删除;加载数据到table,重新生成新的曲目数组及索引数组

        NSMutableArray *newLocalTracks1 = [NSMutableArray array];

        NSMutableArray *newIndexArray = [NSMutableArray array];

        for (NSMutableArray *array in newLocalTracks)

        {

            if ([array count] > 0) {

                [newLocalTracks1 addObject:array];

                [newIndexArray addObject:[[_indexCollation sectionTitles] objectAtIndex:[newLocalTracks indexOfObject:array]]];

            }

        }

        

        _tracksArray = [NSMutableArray arrayWithArray:newLocalTracks1];

        _indexArray = [NSMutableArray arrayWithArray:newIndexArray];

        

        dispatch_async(dispatch_get_main_queue(), ^{

            [_table reloadData];

        });

 

 

/* 返回索引数组 */

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

{

    return _indexArray;

}

 

UITableView 索引的设置,布布扣,bubuko.com

UITableView 索引的设置

标签:des   数据   io   for   cti   ar   new   app   

原文地址:http://www.cnblogs.com/zengyanzhi/p/3872993.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!