UITableView 索引的設定

來源:互聯網
上載者:User

標籤: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;

}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.