#pragma mark -------配置分组信息------
#define NEW_USER(str) [[User alloc] init:str name:str]
- (
void
)configureSections {
userArray = [[
NSMutableArray
alloc] init];
[userArray addObject:NEW_USER(@
"test001"
)];
[userArray addObject:NEW_USER(@
"test002"
)];
[userArray addObject:NEW_USER(@
"test003"
)];
[userArray addObject:NEW_USER(@
"test004"
)];
[userArray addObject:NEW_USER(@
"test005"
)];
[userArray addObject:NEW_USER(@
"adam01"
)];
[userArray addObject:NEW_USER(@
"adam02"
)];
[userArray addObject:NEW_USER(@
"adam03"
)];
[userArray addObject:NEW_USER(@
"bobm01"
)];
[userArray addObject:NEW_USER(@
"bobm02"
)];
[userArray addObject:NEW_USER(@
"what01"
)];
[userArray addObject:NEW_USER(@
"0what02"
)];
[userArray addObject:NEW_USER(@
"李一"
)];
[userArray addObject:NEW_USER(@
"李二"
)];
[userArray addObject:NEW_USER(@
"胡一"
)];
[userArray addObject:NEW_USER(@
"胡二"
)];
self
.collation = [
UILocalizedIndexedCollation
currentCollation];
NSInteger
index, sectionTitlesCount = [[collation sectionTitles] count];
NSMutableArray
*newSectionsArray = [[
NSMutableArray
alloc] initWithCapacity:sectionTitlesCount];
for
(index = 0; index < sectionTitlesCount; index++) {
NSMutableArray
*array = [[
NSMutableArray
alloc] init];
[newSectionsArray addObject:array];
}
for
(User *userObj in userArray) {
NSInteger
sectionNumber = [collation sectionForObject:userObj collationStringSelector:
@selector
(username)];
NSMutableArray
*sectionUserObjs = [newSectionsArray objectAtIndex:sectionNumber];
[sectionUserObjs addObject:userObj];
}
for
(index = 0; index < sectionTitlesCount; index++) {
NSMutableArray
*userObjsArrayForSection = [newSectionsArray objectAtIndex:index];
NSArray
*sortedUserObjsArrayForSection = [collation sortedArrayFromArray:userObjsArrayForSection collationStringSelector:
@selector
(username)];
[newSectionsArray replaceObjectAtIndex:index withObject:sortedUserObjsArrayForSection];
}
self
.sectionsArray = newSectionsArray;
}
#pragma mark --------tableview的委托和datasource-------
- (
NSInteger
)numberOfSectionsInTableView:(
UITableView
*)tableView {
return
[[collation sectionTitles] count];
}
- (
NSInteger
)tableView:(
UITableView
*)tableView numberOfRowsInSection:(
NSInteger
)section {
NSArray
*UserObjsInSection = [sectionsArray objectAtIndex:section];
return
[UserObjsInSection count];
}
- (
UITableViewCell
*)tableView:(
UITableView
*)tableView cellForRowAtIndexPath:(
NSIndexPath
*)indexPath {
static
NSString
*CellIdentifier = @
"Cell"
;
UITableViewCell
*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if
(cell ==
nil
) {
cell = [[
UITableViewCell
alloc] initWithStyle:
UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
NSArray
*userNameInSection = [sectionsArray objectAtIndex:indexPath.section];
User *userObj = [userNameInSection objectAtIndex:indexPath.row];
cell.textLabel.text = userObj.username;
return
cell;
}
- (
void
)tableView:(
UITableView
*)tableView didSelectRowAtIndexPath:(
NSIndexPath
*)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:
YES
];
}
- (
NSString
*)tableView:(
UITableView
*)tableView titleForHeaderInSection:(
NSInteger
)section {
NSArray
*UserObjsInSection = [sectionsArray objectAtIndex:section];
if
(UserObjsInSection ==
nil
|| [UserObjsInSection count] <= 0) {
return
nil
;
}
return
[[collation sectionTitles] objectAtIndex:section];
}
- (
NSArray
*)sectionIndexTitlesForTableView:(
UITableView
*)tableView {
return
[collation sectionIndexTitles];
}
- (
NSInteger
)tableView:(
UITableView
*)tableView sectionForSectionIndexTitle:(
NSString
*)title atIndex:(
NSInteger
)index {
return
[collation sectionForSectionIndexTitleAtIndex:index];
}