iphone开发基础_分组分区_P165_的理解

View Code
//names表示一个Dictionary,拥有Key(A-Z)和Value(值)
//key为Key。 - -
#pragma - 
//返回有多少组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //从A-Z,表示有26行
    return [keys count];
}
//返回每组有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //根据section参数,得到key的值
    NSString *key = [keys objectAtIndex:section];
    //根据得到的key,将得到的Value值组成数组
    NSArray *nameSection = [names objectForKey:key];
    //获取数组的数量,即为每组的行数
    return [nameSection count];
    //此处测试,改变哪行的行数
    
//根据上面(return [keys count])得到的值,此方法需要返回[keys count]个值
    /*
    //修改第一组,为5行,以下按需求修改
    if (section==0) {
        return 5;
    }
    if (section==1) {
        return 4;
    }
    if (section==2) {
        return 3;
    }
    if (section==3) {
        return 2;
    }
    //else都时候1行
    return 1;
    
*/
}
//设置每行的数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //获取行号
    NSUInteger section = [indexPath section];
    //获取行数
    NSUInteger row = [indexPath row];
    //同上,获取key值
    NSString *key = [keys objectAtIndex:section];
    //同上,得到Value值
    NSArray *nameSection = [names objectForKey:key];
    //设置标识
    static NSString *SectionTableIdentifier = @"SectionsTableIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionTableIdentifier];
    if (cell ==nil) {
        cell= [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SectionTableIdentifier] autorelease];
    }
    //设置标签text。将得到的Value值写入每一行(是这么理解么?)
    cell.textLabel.text = [nameSection objectAtIndex:row];
    //返回值。
    return cell;
    //此后执行N次
}
//设置Header标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    //获取Key,并返回
    NSString * key = [keys objectAtIndex:section];
    return key;
}


#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    //此处使用plist文件,作为数据
    
//获取路径,设为字符串
    NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"];
    //创建一个Dicitionary,保存数据
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
    //复制
    self.names = dict;
    //此处需要释放
    [dict release];
    //获取所有的Key,并且排序
    NSArray *array = [[names allKeys] sortedArrayUsingSelector:@selector(compare:)];
    //赋值
    self.keys = array;
    //此处木有释放
    
    [super viewDidLoad];
}

@end

 

如果哪错了,被哪位朋友看见,告知下,谢谢!

 

posted @ 2011-10-19 14:06  Maxfong  阅读(175)  评论(0编辑  收藏  举报