适用范围cell的格式固定
1.将plist文件和图片文件导入到supporting files
2.新建class1 将plist的内容建成模型
3.用empty创建xib文件用storyboard拖动形成固定界面
4.创建class2文件用来封装xib
5.将xib的class属性变成用来封装的class2的文件名
6 .在class2文件中导入class1 这样就可以拿到class1的属性
7.方法重写将用拖线的方式把xib文件的属性拖到calss2 (@interface)
@interface tgCell ()
@property (weak,nonatomic) IBOutletUIImageView *iconView;
@property (weak,nonatomic) IBOutletUILabel *titleView;
@property (weak,nonatomic) IBOutletUILabel *priceView;
@property (weak,nonatomic) IBOutletUILabel *buyCount;
@end
8.在class2中将class1的属性赋值到xib的属性中
-(void)setTgModel:(tg *)tgModel
{
_tgModel = tgModel;//重写成员变量一定要把它重新赋值
self.iconView.image = [UIImageimageNamed:tgModel.icon];
self.titleView.text = tgModel.title;
self.priceView.text = [NSStringstringWithFormat:@"价格 %@",tgModel.price];
self.buyCount.text = [NSStringstringWithFormat:@"%@人已购买",tgModel.buyCount];
}
9.在.m文件中 先创建模型数据
-(NSArray *)tgArr
{
if (_tgArr ==nil) {
NSString *path = [[NSBundlemainBundle]pathForResource:@"tgs.plist"ofType:nil];
NSArray *arr = [NSArrayarrayWithContentsOfFile:path];
NSMutableArray *mArr = [NSMutableArrayarray];
for (NSDictionary *dicin arr) {
tg *tgModel = [tgtgWithDic:dic];
[mArr addObject:tgModel];
}
_tgArr = mArr;
}
return_tgArr;
}
10.再将模型显示
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
returnself.tgArr.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
tgCell *cell = [tgCellcellWithTableView:tableView];
cell.tgModel =self.tgArr[indexPath.row];
return cell;
}
值得一提的是最后一个方法 自定义一个tgcell类的cell
+(instancetype)cellWithTableView:(UITableView *)tableView;
+(instancetype)cellWithTableView:(UITableView *)tableView
{
staticNSString *ID = @"tg";
tgCell *cell = [tableViewdequeueReusableCellWithIdentifier:ID]; //在.m中创建时是一般情况 uitableviewcell 在这里因为你xib中cell是tgcell类型 所以这里也是tgcell类型
if (cell ==nil) {
cell = [[[NSBundlemainBundle]loadNibNamed:@"tgCell"owner:niloptions:nil]lastObject]; //取出来是一个数组
}
return cell;
}
1。创建xib来描述一个view的内部结构
2.新建一个类(继承自某个系统自带的view,继承自哪个类,取决于xib根对象的class)
3.新建类的类名最好跟xib文件名保持一致
4.将xib中的控件和自定义类进行连线
5.提供一个类方法快速返回一个创建好的自定义view(屏蔽xib加载这个过程)