按首字母索引

實際上UITableView默認就支持象電話本那樣的按首字母索引。 實現sectionIndexTitlesForTableView 和 sectionForSectionIndexTitle 這兩個接口即可。 細節請參考UITableViewDataSource幫助文檔。

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

{

NSMutableArray?*toBeReturned = [[NSMutableArray?alloc]init];

for(char?c =?‘A’;c<=‘Z’;c++)

[toBeReturned?addObject:[NSString?stringWithFormat:@"%c",c]];

return?toBeReturned;

}

- (NSInteger)tableView:(UITableView?*)tableView sectionForSectionIndexTitle:(NSString?*)title atIndex:(NSInteger)index

{

NSInteger?count =?0;

for(NSString?*character?in?arrayOfCharacters)

{

if([character?isEqualToString:title])

return?count;

count ++;

}

return?0;

}

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

{

if([arrayOfCharacters?count]==0)

return?@”";

return?[arrayOfCharacters?objectAtIndex:section];

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容