用到UITableView组件,将四个界面中的第一个界面修改如下。(每个组件都有各自的属性及方法)
这个界面用的是UITableView的普通样式,即UITableViewStylePlain。
因为只修改了第一个界面,所以只需在FirstViewController.m中修改代码。
UITableView里面包含了多个UITableViewCell,即一个表格有多行,每行是一个UITableViewCell,想得到这个效果,用到了UITableViewCell的textLabel(显示内容)、detailTextLabel(显示详情)、imageView属性(显示图片)。
生成一个Firsts类,便于管理页面中添加的信息。
Firsts.h代码如下。
#import <Foundation/Foundation.h>
@interface Firsts : NSObject
@property NSString *photo;
@property NSString *name;
@property NSString *date;
+(Firsts *)initwithPhoto:(NSString *)photo
Name:(NSString *)name
Date:(NSString *)date;
@end
Firsts.m代码如下。
#import "Firsts.h"
@implementation Firsts
+(Firsts *)initwithPhoto:(NSString *)photo
Name:(NSString *)name
Date:(NSString *)date{
Firsts *first=[[Firsts alloc]init];
first.photo=photo;
first.name=name;
first.date=date;
return first;
}
@end
修改后的FirstViewController.m代码如下。[注意要在FirstViewController.h文件<>中加入UITableViewDelegate,UITableViewDataSource协议 ]
[注意tableview的重用机制]
#import "FirstViewController.h"
#import "AppDelegate.h"
#import "Firsts.h"
@interface FirstViewController ()
@property NSMutableArray *arrayHomes;
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"首页";
Firsts *first1=[Firsts initwithPhoto:@"photo1.png"
Name:@"Tom"
Date:@"2018.2.3"];
Firsts *first2=[Firsts initwithPhoto:@"photo2.png"
Name:@"Mike"
Date:@"2018.5.23"];
Firsts *first3=[Firsts initwithPhoto:@"photo3.png"
Name:@"Tony"
Date:@"2018.6.3"];
Firsts *first4=[Firsts initwithPhoto:@"photo4.png"
Name:@"Andy"
Date:@"2018.6.17"];
Firsts *first5=[Firsts initwithPhoto:@"photo5.png"
Name:@"Frank"
Date:@"2018.4.13"];
Firsts *first6=[Firsts initwithPhoto:@"photo6.png"
Name:@"Nancy"
Date:@"2018.5.23"];
Firsts *first7=[Firsts initwithPhoto:@"photo7.png"
Name:@"Bruce"
Date:@"2018.4.13"];
_arrayHomes=[[NSMutableArray alloc]initWithObjects:first1,first2,first3,first4,first5,first6,first7, nil] ;
UITableView *homeTable=[[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
[homeTable setRowHeight:80]; //行高
[homeTable setSeparatorColor:[UIColor brownColor]]; //分割线颜色
UIEdgeInsets inset = homeTable.separatorInset;
homeTable.separatorInset = UIEdgeInsetsMake(inset.top, 0, inset.bottom, inset.right); //0表示距左边界的距离
homeTable.delegate=self;
homeTable.dataSource=self;
//设置数据源代理,即从self获取数据
[self.view addSubview:homeTable];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ //这一组有多少行
return _arrayHomes.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath{
//每一行显示什么内容,其中,indexPath是对应的第几行
static NSString *identifier = @"identifier";
UITableViewCell *home = [tableView dequeueReusableCellWithIdentifier:identifier];
if (home == nil) {
home = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
} //这部分代码体现了UITableViewCell的重用机制, 重用的话,使得占用的内存更小
Firsts *f=[_arrayHomes objectAtIndex:indexPath.row];
home.imageView.image=[UIImage imageNamed:f.photo]; //设置图片
home.textLabel.text=f.name; //设置文字
home.detailTextLabel.text=f.date; //设置详细文字
return home;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
至此,基本完成。