顺便把索引和tableView dataSource的代理方法也贴一下:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.contacts.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.contacts[section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"contactCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.imageView.image = [UIImage imageNamed:@"default_head"];
cell.textLabel.text = [self.contacts objectAtIndex:indexPath.section][indexPath.row];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [_indexArray objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return _indexArray;
}
//索引列点击事件
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return index;
}
还有两个很重要的方法:
下面这个方法是[theCollation sectionForObject:p collationStringSelector:@selector(getFirstLetter)]; 是这里的p对象要实现的方法,我这里的p是NSString,你也可以用其他对象例如Person。
NSString *ret = @"";
if (![self canBeConvertedToEncoding: NSASCIIStringEncoding]) {//如果是英语
if ([[self letters] length]>2) {
ret = [[self letters] substringToIndex:1];
}
}
else {
ret = [NSString stringWithFormat:@"%c",[self characterAtIndex:0]];
}
return ret;
}
下面这个方法是NSString得类别方法
- (NSString *)letters{
NSMutableString *letterString = [NSMutableString string];
int len = [self length];
for (int i = 0;i < len;i++)
{
NSString *oneChar = [[self substringFromIndex:i] substringToIndex:1];
if (![oneChar canBeConvertedToEncoding:NSASCIIStringEncoding]) {
NSArray *temA = makePinYin2([oneChar characterAtIndex:0]);
if ([temA count]>0) {
oneChar = [temA objectAtIndex:0];
}
}
[letterString appendString:oneChar];
}
return letterString;
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
注:相关教程知识阅读请移步到IOS开发频道。










