uitableview中文排序问题

1,uitableview中涉及到排序的问题,查找资料后发现使用UILocalizedIndexedCollation可以很好处理中文和英文系统下中文的排序。而且如果第一个汉字首字母一样那么就会按照第二个开始排序。

2,如果tableview的高度不是整个全屏的,比如有导航栏定制等,那么ios7.0下会缩减显示。呈现的是带有圆点的效果。其他系统不会。且无法修改。

效果如下:

          

 

代码如下:

#pragma mark -
#pragma mark Table view data source and delegate methods
//设置Section的数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // The number of sections is the same as the number of titles in the collation.
    return [[collation sectionTitles] count];
}

//设置每个Section下面的cell数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    // The number of time zones in the section is the count of the array associated with the section in the sections array.
    NSArray *friendsInSection = [sectionsArray objectAtIndex:section];
    
    return [friendsInSection count];
}

//设置每行的cell的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    self.customTable = tableView;
    static NSString *CellIdentifier = @"Friends";
    
    UITableViewCell *cell = [self.customTable dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;

    }
    
    // Get the time zone from the array associated with the section index in the sections array.
    NSArray *friendsInSection = [sectionsArray objectAtIndex:indexPath.section];
    
    // Configure the cell with the time zone's name.
    FriendsWrapper *friend = [friendsInSection objectAtIndex:indexPath.row];
    cell.textLabel.text = friend.localeName;
    
    return cell;
}

/*
 Section-related methods: Retrieve the section titles and section index titles from the collation.
 */

-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
    UITableViewCell*cell =[self tableView:tableView cellForRowAtIndexPath:indexPath];
    
    return cell.frame.size.height;
}

//设置section的Header
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    //NSLog(@"%d",[[self.sectionsArray objectAtIndex:section]count]);
        if ([[self.sectionsArray objectAtIndex:section]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];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark -
#pragma mark Set the data array and configure the section data

- (void)setFriendsListArray:(NSMutableArray *)newDataArray {
    if (newDataArray != friendsListArray) {
        [friendsListArray release];
        friendsListArray = [newDataArray retain];
    }
    if (friendsListArray == nil) {
        self.sectionsArray = nil;
    }
    else {
        [self configureSections];
    }
}

- (void)configureSections {
    
    //获得当前UILocalizedIndexedCollation对象并且引用赋给collation
    self.collation = [UILocalizedIndexedCollation currentCollation];
    //获得索引数和section标题数
    NSInteger index, sectionTitlesCount = [[collation sectionTitles] count];
    
    NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
    
    //设置sections数组:元素包含timezone
    for (index = 0; index < sectionTitlesCount; index++) {
        NSMutableArray *array = [[NSMutableArray alloc] init];
        [newSectionsArray addObject:array];
        [array release];
    }
    
    // Segregate the time zones into the appropriate arrays.
    for (FriendsWrapper *friend in friendsListArray) {
        
        //根据timezone的localename,获得对应的时区的section number
        NSInteger sectionNumber = [collation sectionForObject:friend collationStringSelector:@selector(localeName)];
        
        //获得section的数组
        NSMutableArray *sectionFriends = [newSectionsArray objectAtIndex:sectionNumber];
        
        //添加时区内容到section中 sectionTimeZones 存放的是所有的字符串
        [sectionFriends addObject:friend];
    }
    
    //排序
    for (index = 0; index < sectionTitlesCount; index++)
   
    {
        
        NSMutableArray *friendsListArrayForSection = [newSectionsArray objectAtIndex:index];
        
        //获得排序结果
        NSArray *sortedFriendsArrayForSection = [collation sortedArrayFromArray:friendsListArrayForSection collationStringSelector:@selector(localeName)];
        
        //替换原来数组
        [newSectionsArray replaceObjectAtIndex:index withObject:sortedFriendsArrayForSection];
    }
    
    self.sectionsArray = newSectionsArray;
    [newSectionsArray release];
}

#import "FriendsWrapper.h"

@implementation FriendsWrapper

@synthesize localeName;

- (id)initWithFriends:(NSString *)nameComponents
{
    
    if (self = [super init]) {
        
        NSString *name = nil;
        
        if ((nameComponents == nil) ||
            (nameComponents.length == 0)){
            
            name = @"";
        }else
        {
            name = nameComponents;
        }
        /*if ([nameComponents count] == 2) {
         name = [nameComponents objectAtIndex:1];
         }
         if ([nameComponents count] == 3) {
         name = [NSString stringWithFormat:@"%@ (%@)", [nameComponents objectAtIndex:2], [nameComponents objectAtIndex:1]];
         }*/
        
        localeName = [[name stringByReplacingOccurrencesOfString:@"_" withString:@" "] retain];
    }
    return self;
}


- (void)dealloc {
    [localeName release];    
    [super dealloc];
}

@end

你可能感兴趣的:(UITableView)