0%

IOS开发 分组显示TableView

IOS中可以分组显示TableView,效果类似于“设置”程序。 新建项目的步骤不再重复,请参考前文。有一点区别在于,需要把Table View的Style属性设置成Grouped(在Attributes Inspector中)。这里需要先写个plist文件,用于存储需要显示的数据。 plist文件本身对应NSDictionary数据类型,也就是说plist文件,其实是个字典。本例plist文件名为sortednames.plist,内容及结构如下图: 两个字符串数组,键名分别为A,B,当然,你也可以在程序中用代码写NSDictionary,这样就不需要sortednames.plist。 PDViewController.h

#import 

@interface PDViewController : UIViewController
@property (strong,nonatomic) NSDictionary *names;
@property(strong,nonatomic)NSArray *keys;

@end

PDAppDelegate.m

#import "PDViewController.h"
#import "NIBCell.h"

@implementation PDViewController
@synthesize names=_names,keys=_keys;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *path=[[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"];
    //取得sortednames.plist绝对路径
    //sortednames.plist本身是一个NSDictionary,以键-值的形式存储字符串数组
    
    NSDictionary *dict=[[NSDictionary alloc] initWithContentsOfFile:path];
    //转换成NSDictionary对象
    self.names=dict;
    NSArray *array=[[_names allKeys] sortedArrayUsingSelector:@selector(compare:)];
    //取得字典中所有的key,使用compare方法(必须是返回NSComparisonResult的方法)排序
    //这里取得的key,对应的值是Array
    _keys=array;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.names=nil;
    self.keys=nil;
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //返回分组数量,即Array的数量
    return [_keys count];

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //返回第section个分组的行数
    NSString *key=[_keys objectAtIndex:section];
    //取得key
    NSArray *nameSection=[_names objectForKey:key];
    //根据key,取得Array
    return [nameSection count];
    //返回Array的大小
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSUInteger section=[indexPath section];
    //分组号
    NSUInteger rowNumber=[indexPath row];
    //行号
    //即返回第section组,rowNumber行的UITableViewCell
    
    NSString *key=[_keys objectAtIndex:section];
    //取得第section组array的key
    NSArray *nameSection=[_names objectForKey:key];
    //通过key,取得Array
    
    static NSString * tableIdentifier=@"CellFromNib";

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:tableIdentifier];
    if(cell==nil)
    {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier];
        
    }
    cell.textLabel.text=[nameSection objectAtIndex:rowNumber]; 
    //从数组中读取字符串,设置text
    return cell;
}

@end