iOS

iOS tableview的Demo

2017-03-07  本文已影响116人  subject_2619
Menu.plist文件

把数据全部存在plist文件中,最外面的数组包含2个字典,字典里面分别保存title与car,car是一个数据,里面有2个字典,字典里面分别保存了车的名字与图片的名字。接下来,我们要构建数据模型,把每一行当作一个模型,里面有车的名字与图片:

```

#import@interface CarModule : NSObject

//名字

@property (nonatomic,copy) NSString *name;

//图片

@property (nonatomic,copy) NSString *icon;

+(instancetype)carWithDict:(NSDictionary*)dic;

-(instancetype)initWithDict:(NSDictionary*)dic;

@end

#import "CarModule.h"

@implementation CarModule

+(instancetype)carWithDict:(NSDictionary *)dic{

return [[self alloc]initWithDict:dic];

}

```

-(instancetype)initWithDict:(NSDictionary *)dic{

if (self=[super init]) {

[self setValuesForKeysWithDictionary:dic];

}

return self;

}

@end

接下来创建每一个区域的模型,

#import@interface CarGroup : NSObject

//标题

@property (nonatomic,copy)NSString *title;

//车的数组

@property (nonatomic,copy)NSArray *carGroupArray;

+(instancetype)carGroupWithDict:(NSDictionary*)dic;

-(instancetype)initWithDict:(NSDictionary*)dic;

@end

#import "CarGroup.h"

#import "CarModule.h"

@implementation CarGroup

+(instancetype)carGroupWithDict:(NSDictionary *)dic{

return [[self alloc]initWithDict:dic];

}

-(instancetype)initWithDict:(NSDictionary *)dic{

if (self=[super init]) {

self.title=dic[@"title"];

self.carGroupArray=dic[@"car"];

NSMutableArray *carArray=[NSMutableArray array];

for (NSDictionary *dic in _carGroupArray) {

CarModule *carModule=[CarModule carWithDict:dic];

[carArray addObject:carModule];

}

}

return self;

}

@end

接下来就是填充数据,这里用到了懒加载

#import "CarTableViewViewController.h"#import "CarGroup.h"#import "CarModule.h"@interface CarTableViewViewController()@end

@implementation CarTableViewViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

//    [self menuData];

tableViewDemo=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height/2-10) style:UITableViewStylePlain];

tableViewDemo.dataSource=self;

[[self view]addSubview:tableViewDemo];

}

//数据懒加载

-(NSMutableArray*)menuData{

if (!menuData) {

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Menu" ofType:@"plist"];

NSArray *data=[NSArray arrayWithContentsOfFile:plistPath];

menuData=[NSMutableArray array];

for (NSDictionary *dic in data) {

CarGroup *carGroup=[CarGroup carGroupWithDict:dic];

[menuData addObject:carGroup];

}

}

return menuData;

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

return self.menuData.count;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

CarGroup *carGroup=self.menuData[section];

return carGroup.carGroupArray.count;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *ID=@"car";

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];

if (cell==nil) {

cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];

}

CarGroup *carGroup=self.menuData[indexPath.section];

CarModule *car=carGroup.carGroupArray[indexPath.row];

cell.imageView.image=[UIImage imageNamed:[car valueForKey:@"icon"]];

cell.textLabel.text=[car valueForKey:@"name"];

return cell;

}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

CarGroup *carGroup=self.menuData[section];

return carGroup.title;;

}

```

上一篇 下一篇

猜你喜欢

热点阅读