利用Xib进行实现
应用场景:像团购网站的列表数据显示,新闻列表显示等(由于该类的显示的数据单元格内容格式相同)
(1)主控制器文件,在文件中实现了自己自定义的代理,加载数据,
1#import"SLViewController.h"2#import"SLTgDatas.h"3#import"SLTableViewCell.h"4#import"SLFooterView.h"5#import"SLHeaderView.h"67@interfaceSLViewController ()<UITableViewDataSource,UITableViewDelegate,SLFooterViewDelegate>89@PRoperty (weak, nonatomic) IBOutlet UITableView *tableview;1011@property (nonatomic,strong) NSMutableArray *arrayM;1213@end1415@implementationSLViewController1617-(void)loadMoreData18{19NSLog(@"=======");20SLTgDatas *da=[[SLTgDatas alloc] init];21da.title=@"西红柿鸡蛋";22da.price=@"12";23da.buyCount=@"56";24da.icon=@"2c97690e72365e38e3e2a95b934b8dd2";25[self.arrayM addObject:da];26[self.tableview reloadData];2728}293031#pragmamark -解析plist数据文件32-(NSMutableArray *)arrayM33{34if(_arrayM==nil) {35NSString *fullpath=[[NSBundle mainBundle] pathForResource:@"tgs"ofType:@"plist"];36NSArray *array=[NSArray arrayWithContentsOfFile:fullpath];37NSMutableArray *arr=[NSMutableArray arrayWithCapacity:array.count];38for(NSDictionary *dictinarray) {39SLTgDatas *data=[SLTgDatas tgDataWithDiectionary:dict];40[arr addObject:data];41}4243_arrayM=arr;44}4546return_arrayM;47}4849- (void)viewDidLoad50{51[super viewDidLoad];52//self.tableview.dataSource=self;5354UINib *nib=[UINib nibWithNibName:@"SLFooterView"bundle:nil];55SLFooterView *footerview=[[nib instantiateWithOwner:nil options:nil] firstObject];56self.tableview.tableFooterView=footerview;5758footerview.delegate=self;5960SLHeaderView *headerview=[SLHeaderView headerWithView];61self.tableview.tableHeaderView=headerview;6263}6465#pragmamark -填充数据进行显示66-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView67{68return1;69}7071-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section72{73returnself.arrayM.count;74}7576-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath77{78SLTableViewCell *cell=[SLTableViewCell cellWithTabelViewCell:tableView];79SLTgDatas *data=self.arrayM[indexPath.row];80cell.data=data;81returncell;82}8384#pragmamark -设置状态栏隐藏85-(BOOL)prefersstatusBarHidden86{87returnYES;88}8990@end
(2)该文件是字典转对象模型文件
1#import<Foundation/Foundation.h>23#import"SLGlobalCode.h"45@interfaceSLTgDatas : NSObject67@property (nonatomic,copy) NSString *title;8@property (nonatomic,copy) NSString *icon;9@property (nonatomic,copy) NSString *price;10@property (nonatomic,copy) NSString *buyCount;1112@property (nonatomic,strong,readonly) UIImage *image;1314//SLTg(tg)15-(instancetype)initWithTgDirectionary:(NSDictionary *)dict;1617+(instancetype)tgDataWithDiectionary:(NSDictionary *)dict;18@end
1#import"SLTgDatas.h"23@interfaceSLTgDatas ()4{5UIImage *_image;6}7@end89@implementationSLTgDatas1011-(UIImage *)image12{13if(_image==nil) {14_image=[UIImage imageNamed:self.icon];15}16return_image;17}18/**19* 对代码进行抽取,成为其他地方也可以用这个方法20*/21//SLTgRetrun(tg)22-(instancetype)initWithTgDirectionary:(NSDictionary *)dict23{24if(self=[self init]) {25[self setValuesForKeysWithDictionary:dict];26}27returnself;28}29+(instancetype)tgDataWithDiectionary:(NSDictionary *)dict30{31return[[self alloc] initWithTgDirectionary:dict];32}33@end
(3)此文件是自定义cell对象,通过xib进行设计后,通过连线进行相关,方便控制器调用
1#import<UIKit/UIKit.h>23#import"SLTgDatas.h"45@interfaceSLTableViewCell : UITableViewCell67@property (nonatomic,strong) SLTgDatas *data;89+(instancetype)cellWithTabelViewCell:(UITableView *)table;1011@end
#import"SLTableViewCell.h"@interfaceSLTableViewCell()
@property (weak, nonatomic) IBOutlet UIImageView*cellImage;
@property (weak, nonatomic) IBOutlet UILabel*celltitle;
@property (weak, nonatomic) IBOutlet UILabel*cellprice;
@property (weak, nonatomic) IBOutlet UILabel*cellbuycount;@end@implementationSLTableViewCell+(instancetype)cellWithTabelViewCell:(UITableView *)table
{staticNSString *str=@"cell";
SLTableViewCell*cell=[table dequeueReusableCellWithIdentifier:str];if(cell==nil) {
cell=[[[NSBundle mainBundle] loadNibNamed:@"SLTgPlistView"owner:nil
options:nil] firstObject];
}returncell;
}-(void)setData:(SLTgDatas *)data
{
_data=data;
self.cellbuycount.text=data.buyCount;
self.cellImage.image=data.image;
self.cellprice.text=data.price;
self.celltitle.text=data.title;
}@end
(4)是底部加载更多选项
1#import<UIKit/UIKit.h>23@protocolSLFooterViewDelegate <NSObject>45-(void)loadMoreData;67@end89@interfaceSLFooterView : UIView1011@property (nonatomic,weak)id<SLFooterViewDelegate>delegate;12@end
1#import"SLFooterView.h"2@interfaceSLFooterView ()34@property (weak, nonatomic) IBOutlet UIButton *clieckbt;5@property (weak, nonatomic) IBOutlet UIView *jiazai;6789@end10@implementationSLFooterView1112-(void)setDelegate:(id<SLFooterViewDelegate>)delegate13{14_delegate=delegate;15}1617-(IBAction)btnclick {18self.clieckbt.hidden=YES;19self.jiazai.hidden=NO;2021//dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.022//* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{23//24//});25//26dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{27if([self.delegaterespondsToSelector:@selector(loadMoreData)])28{29[self.delegateloadMoreData];30}31self.clieckbt.hidden=NO;32self.jiazai.hidden=YES;33});343536}3738@end
以上就是利用xib进行设计的tableView进行显示的列表数据
综上所述:在自定义UITabelViewCell的时候,有两种方式,要根据不同的场景进行利用。