MVC模式

MVC:model层:数据源,提供数据 .controller层:控制(中介).view层:视图负责显示数据
model层----StudentHandle
.h文件

+ (StudentHandle *)sharedStudentHandle;
+ (NSInteger)numberOfSectionsInTableView;
+ (NSInteger)numberOfRowsInSection:(NSInteger)section;
+ (NSString *)titleForHeaderInSection:(NSInteger)section;
+ (NSArray *)sectionIndexTitlesForTableView;
+ (Student *)getStudentAtIndexPath:(NSIndexPath
.m文件*)indexPath;

//在延展中声明属性

@interface StudentHandle ()
@property (nonatomic,retain)NSMutableDictionary *stuDic;
@property (nonatomic,retain)NSArray *orderKeys;
@end

static StudentHandle *sh =nil;
+ (StudentHandle *)sharedStudentHandle
{
    if (nil == sh) {
        sh = [[StudentHandle alloc]init];
        [sh queryData];//把数据放到初始化中.
    }
    return sh;
}
//处理数据
- (void)queryData
{

    NSString *imagePath = [[NSBundle mainBundle]pathForResource:@"Student" ofType:@"plist"];
    self.stuDic = [[NSMutableDictionary alloc]initWithContentsOfFile:imagePath];
    
    NSArray *unorderKeys = [_stuDic allKeys];
    self.orderKeys = [unorderKeys sortedArrayUsingSelector:@selector(compare:)];
    

    for (NSString *key in _orderKeys) {
        NSArray *stuArr = [_stuDic objectForKey:key];
        
        NSMutableArray *groupArr = [[NSMutableArray alloc]initWithCapacity:[stuArr count]];
        for (NSDictionary *tempDic in stuArr) {
            Student *stu = [[Student alloc]initWithName:[tempDic objectForKey:@"name"] sex:[tempDic objectForKey:@"sex"] age:[tempDic objectForKey:@"age"] phone:[tempDic objectForKey:@"phone"] imageName:[tempDic objectForKey:@"imageName"]];
            [groupArr addObject:stu];
            [stu release];
        }
        

//替换原来key对应的value(数组).

[_stuDic setValue:groupArr forKey:key]; [groupArr release]; } } //获取分区数 + (NSInteger)numberOfSectionsInTableView { return [[StudentHandle sharedStudentHandle].orderKeys count]; } //获取行数 + (NSInteger)numberOfRowsInSection:(NSInteger)section { NSString *key = [StudentHandle titleForHeaderInSection:section]; return [[[StudentHandle sharedStudentHandle].stuDic objectForKey:key]count]; } //获取分区的title + (NSString *)titleForHeaderInSection:(NSInteger)section { return [[StudentHandle sharedStudentHandle].orderKeys objectAtIndex:section]; } //获取快速索引 + (NSArray *)sectionIndexTitle ForTableView { return [StudentHandle sharedStudentHandle].orderKeys; } //获取学生对象 + (Student *)getStudentAtIndexPath:(NSIndexPath *)indexPath { NSString *key = [[StudentHandle sharedStudentHandle].orderKeys objectAtIndex:indexPath.section]; NSArray *stuArr = [[StudentHandle sharedStudentHandle].stuDic objectForKey:key]; return [stuArr objectAtIndex:indexPath.row]; } -(void)dealloc { [_stuDic release]; [_orderKeys release]; [super dealloc]; } model层---Student .h文件 @property (nonatomic,copy)NSString *name; @property (nonatomic,copy)NSString *sex; @property (nonatomic,copy)NSString *age; @property (nonatomic,copy)NSString *phone; @property (nonatomic,copy)NSString *imageName; - (id)initWithName:(NSString *)name sex:(NSString *)sex age:(NSString *)age phone:(NSString *)phone imageName:(NSString *)imageName; //只传入一个字典参数就可以了,外界在使用的时候就非常方便(封装). - (id)initWithDic:(NSDictionary *)dic; .m文件 - (id)initWithName:(NSString *)name sex:(NSString *)sex age:(NSString *)age phone:(NSString *)phone imageName:(NSString *)imageName { self = [super init]; if ( self) { self.name = name; self.age = age; self.sex = sex; self.phone =phone; self.imageName = imageName; } return self; // - (id)initWithDic:(NSDictionary *)dic //{ //self = [super init]; //if (self) { // self.name = [dic objectForKey:@"name"]; // self.sex = [dic objectForKey:@"sex"]; // self.age = [dic objectForKey:@"age"]; // self.phone = [dic objectForKey:@"phone"]; // self.imageName = [dic objectForKey:@"imageName"]; //} //return self; } } - (void)dealloc { [_name release]; [_age release]; [_sex release]; [_phone release]; [_imageName release]; [super dealloc]; } Controller层--RootViewController .h文件 @interface RootViewController:UIViewController<UITableViewDataSource,UITableViewDelegate]]> .m文件 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [StudentHandle numberOfRowsInSection:section]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [StudentHandle numberOfSections]; } - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView; { return [StudentHandle sectionIndexTitles]; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return [StudentHandle titleForHeaderInSection:section]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *reuserIdentifier= @"reuse"; StudentCell *cell = [tableView dequeueReusableCellWithIdentifier:reuserIdentifier]; if (nil == cell) { cell = [[[StudentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuserIdentifier]autorelease]; } Student *stu = [StudentHandle getStudentAtIndexPath:indexPath]; [cell configureCellWithStudent:stu]; return cell; } View层--StudentCell .h文件 @interface StudentCell : UITableViewCell @property (nonatomic,retain)UILabel *nameLabel; @property (nonatomic,retain)UILabel *sexLabel; @property (nonatomic,retain)UILabel *ageLabel; @property (nonatomic,retain)UILabel *phoneLabel; @property (nonatomic,retain)UIImageView *photoImage; - (void)configureCellWithStudent:(Student *)student; @end .m文件 #define MARGIN_TOP 5 #define MARGIN_LEFT 30 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code //_nameLabel self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(MARGIN_LEFT, MARGIN_TOP, 130, 30)]; [self.contentView addSubview:_nameLabel]; [_nameLabel release]; //_sexLabel self.sexLabel = [[UILabel alloc]initWithFrame:CGRectMake(MARGIN_LEFT+5, MARGIN_TOP+35, 60, 30)]; [self.contentView addSubview:_sexLabel]; [_sexLabel release]; //_ageLabel self.ageLabel = [[UILabel alloc]initWithFrame:CGRectMake(MARGIN_LEFT+50, MARGIN_TOP+35, 50, 30)]; [self.contentView addSubview:_ageLabel]; [_ageLabel release]; //_phoneLabel self.phoneLabel = [[UILabel alloc]initWithFrame:CGRectMake(MARGIN_LEFT, MARGIN_TOP+55, 130, 30)]; [self.contentView addSubview:_phoneLabel]; [_phoneLabel release]; //_photoImage self.photoImage = [[UIImageView alloc]initWithFrame:CGRectMake(MARGIN_LEFT+170, MARGIN_TOP, 80, 80)]; [self.contentView addSubview:_photoImage]; [_photoImage release]; } return self; } -(void)dealloc { [_nameLabel release]; [_sexLabel release]; [_ageLabel release]; [_phoneLabel release]; [_photoImage release]; [super dealloc]; } - (void)configureCellWithStudent:(Student *)stu { self.nameLabel.text = stu.name; self.sexLabel.text = stu.sex; self.ageLabel.text = stu.age; self.phoneLabel.text = stu.phone; NSString *imagePath = [[NSBundle mainBundle]pathForResource:stu.imageName ofType:nil]; self.photoImage.image = [UIImage imageWithContentsOfFile:imagePath]; }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值