在这个地方我们通过一个例子来解释怎样创建一个动态的表格:
第一步:创建一个项目,并且设置屏幕的大小,然后将一向ViewController中拖动一个TableView这个控件,居中,并且屏幕全覆盖哦。
第二步:将我们的素材拖到项目中一个放到Images,另外的一个文件放在team.plist中
第三步:我们要做的是讲数据放到项目中,那么自然要创建一个对象对象,用来盛放这些数据。
第四步:对于我们创建的对象,我们可以肯定的是里面应该有某些属性用来盛放数据,因此我们要定义构造函数和类的构造函数@interface Team : NSObject
-(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)teamWithDict:(NSDictionary *)dict;
@end
第五步:我们定义的类的m文件中创建定义属性,并且实现两个构造函数
#import "Team.h"
@interface Team()
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *image;
@end
@implementation Team
-(instancetype)initWithDict:(NSDictionary *)dict
{
if(self=[super init])
{
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+(instancetype)teamWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
@end
第六步:在ViewContorller.m中我们还要这个文件中实现一个协议,也就是<UITableViewDataSource>这个协议,并且实现里面的两个方法。这两个方法是对里面的一个里面的TableView的一个布局,第一个方法的返回值是一个数数字,表示的是我们的这个tableView的我们看到的是有多少个行。然后第二个是返回的是一个cell是让我们得到一个cell,这样我们就可以对每一行进行操作,其实这一步可以认为是可视化的操作,也就是实现一个布局:
@interface ViewController ()<UITableViewDataSource>
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//这个函数的返回值是cell的数目
return 0;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{//这个函数的返回值是一个cell我们队每一个cell操作的时候都是通过这个cell给里面添加内容的。
return nil;
}
下面我们要做的是从plist文件中获取到我们需要的数据,怎样获取数据,我们获取的数据最后是要存放在一个数组中,也就是我们需要做的是定义一个数组,然后重写get方法,因为我们是从文件中获取数据,所以要重写该数组的get方法:
#import "ViewController.h"
#import "Team.h"
@interface ViewController ()<UITableViewDataSource>
@property(nonatomic,strong)NSMutableArray *teams;
@end
@implementation ViewController
//这个地方是重写一个get方法,我们在重写get方法的时候也是注意到一一些问题:
-(NSMutableArray*)teams
{
//首先是判断这个数组是不是为空,如果这个数组为空就执行其他的操作
if(_teams==nil)
{
//创建一个数组,用来盛放每一个数据
_teams=[NSMutableArray array];
//Bundle是一个目录,其中包含的程序会使用到的资源,这些资源包括图像,声音编译好的代码。我们的程序叫做bundle 在Finder中一个程序,看上去和其它的文件并没有什么区别,但是是实际上是她包含了一个nib的文件,编译代码,以及其他的资源的目录,我们把这个目录叫做main bundle,也就是说我们通过代码是可以获得得到程序的main bundle
// NSBundle *myBundle=[NSBundle mainBundle];
//我们是从文件中获取数据,所以这一步可以认为是必须的
NSString *file=[[NSBundle mainBundle] pathForResource:@"team.plist" ofType:nil];
//我们需要从文件中获取数组
NSArray *arr=[NSArray arrayWithContentsOfFile:file];
for(NSDictionary *dict in arr)
{
//获取team,也就是一个数据对象
Team *team=[Team teamWithDict:dict];
//获取之后将这个数组对象放到数组中
[_teams addObject:team];
}
}
return _teams;
}
//获得这些数据之后我们要做的是重新的完善一下刚才我们我们需要定义的两个协议的方法,因为我们知道了数据有多少个,因此我们可以通过数据知道需要多少行,第二个是我们知道了又多少行,当然知道对象了也明白每一行应该写那些内容了。
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.teams.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Team *team=[self.teams objectAtIndex:indexPath.row];
//我们应该创建一个cell
UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
//向cell中添加文字
[cell.textLabel setText:team.name];
//向cell中添加图片
[cell.imageView setImage:[UIImage imageNamed:team.image]];
return cell;
}
最后一步是将dataSource执行viewControl这个是在view上面进行操作的。