root.h
#import <UIKit/UIKit.h> NS_ASSUME_NONNULL_BEGIN @interface RootViewController : UIViewController @end NS_ASSUME_NONNULL_END
root.m
#import "RootViewController.h" #import "Movie.h" @interface RootViewController ()<UITableViewDataSource,UITableViewDelegate> { NSMutableArray *array; } @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; UITableView *tab=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 375, 667-64) style:UITableViewStylePlain]; [self.view addSubview:tab]; tab.delegate=self; tab.dataSource=self; //添加表头 UIImageView *imageV=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 375, 80)]; imageV.image = [UIImage imageNamed:@"222"]; tab.tableFooterView=imageV; //数据源数组 // array =[NSArray arrayWithObjects:@"亚瑟",@"剑圣",@"盲僧",@"老布",@"老牛", nil]; array =[NSMutableArray array]; //初始化 Movie* m1=[[Movie alloc]init]; m1.movieName=@"金"; m1.movieActor=@"张大大"; Movie* m2=[[Movie alloc]init]; m2.movieName=@"金"; m2.movieActor=@"张大大"; //将电影添加到数组中 [array addObject:m1]; [array addObject:m2]; } //UITableView 代理方法 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //1.创建静态标识符 static NSString *identifier =@"cell"; //2.根据标识符从重用池中取cell UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier]; //3.如果没有取到就创建一个新的 if (cell==nil){ NSLog(@"进来了"); cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier]; } //对cell进行赋值 // cell.textLabel.text =[array objectAtIndex:indexPath.row]; // cell.textLabel.text=@"老罗"; // cell.detailTextLabel.text=@"老罗这个人很屌"; //这样写就很好 Movie* tempMovie=[array objectAtIndex:indexPath.row]; cell.textLabel.text=tempMovie.movieName; cell.detailTextLabel.text=tempMovie.movieActor; cell.imageView.image=[UIImage imageNamed:@"tianmao"]; cell.accessoryType=UITableViewCellAccessoryDetailButton; // cell.accessoryView= return cell; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return array.count; } //cell的高度 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 60; } //控制分区的代理方法(亚州 ,欧美 ,日韩 ) //分区的个数 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 3; } //分区的高度 控制高度 -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 60; } //分区标题 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { if(section==0){ return @"亚洲"; }else if(section==1){ return @"欧美"; }else{ return @"日韩"; } } //自定义分区 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 40)]; view.backgroundColor=[UIColor redColor]; return view; }
movie.h
#import <Foundation/Foundation.h> NS_ASSUME_NONNULL_BEGIN @interface Movie : NSObject @property(nonatomic,strong)NSString* movieName; @property(nonatomic,strong)NSString* movieActor; @end