![](/img/trans.png)
[英]iPhone How to sort Plist data and How to sorted data Display On UITableView
[英]sort data and display in UItableview on iphone
我正在使用此查詢按ASC順序獲取數據,請從list_tbl中選擇*,其中cat_id =%d按名稱ASC排序
現在我的表格視圖顯示基於ASC順序的數據,然后我用它來獲取右側從A到Z的字母
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)help
{
return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:help];
}
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}
現在我的問題是如何根據節標題對數據進行划分,就像所有A內容都轉到A,然后B到Z一樣,當用戶單擊右側的任何字母時,應該轉到該節
// 更新 ///////////////
-(void)setUpRecordsForIndexing
{
for (NSString * recordName in appDelegate.catLists)//where app.catList contains all the value
{
NSString *firstLetter = ([recordName length] ==0)?miscKey:[[recordName substringToIndex:1]uppercaseString];
/*if(![firstLetter beginsWithCharacterInSet:[NSCharacterSet letterCharacterSet]] || [recordName isEqualToString:@""] || recordName == nil )
firstLetter = miscKey ;
*/
NSMutableArray *indexArray = [IndexedRecords objectForKey:firstLetter];
if (indexArray == nil)
{
indexArray = [[NSMutableArray alloc] init];
[IndexedRecords setObject:indexArray forKey:firstLetter];
[IndexLetters addObject:firstLetter];
[indexArray release];
}
[indexArray addObject:record];// this is not workking
// NSLog(@" index array is %@",indexArray);
}
}
我的INDEXRECORD沒有顯示正確的值,我現在可以根據ABCD划分該部分,但每個部分的內容都相同,我在做什么錯
例如:說RecordsArray包含您的所有記錄,現在在表視圖數據源方法numberOfSectionsInTableView
調用另一個方法為您的所有記錄建立索引,如下所示
-(void)setUpRecordsForIndexing
{
if(IndexedRecords)
{
[IndexedRecords release];
IndexedRecords = nil;
}
if(IndexLetters)
{
[IndexLetters release];
IndexLetters = nil;
}
IndexedRecords = [[NSMutableDictionary alloc] init];
IndexLetters = [[NSMutableArray alloc] init];
NSString *miscKey = @"#";
for (NSString * recordName in RecordsArray)
{
NSString *firstLetter = ([recordName length] ==0)?miscKey:[[recordName substringToIndex:1]uppercaseString];
if(![firstLetter beginsWithCharacterInSet:[NSCharacterSet letterCharacterSet]] || [recordName isEqualToString:@""] || recordName == nil )
firstLetter = miscKey ;
NSMutableArray *indexArray = [mIndexedRecords objectForKey:firstLetter];
if (indexArray == nil)
{
indexArray = [[NSMutableArray alloc] init];
[IndexedRecords setObject:indexArray forKey:firstLetter];
[IndexLetters addObject:firstLetter];
[indexArray release];
}
[indexArray addObject:record];
}
}
並且在numberOfSectionsInTableView
返回IndexLetters
的計數。 我希望這能解決您的問題。
在為記錄建立索引時,請將記錄保存在字典中,例如IndexedRecords。 這樣,對於鍵“ A”,設置記錄數組以字母“ A”開頭。 並且在填充表格視圖時
[[IndexedRecords objectForKey:[IndexLetters objectAtIndex:indexPath.section]]objectAtIndex:indexPath.row]
IndexLetters也是一個數組,其中包含當前列表的索引字母。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.