本文示範如何用 xib 設計自訂的 UITableViewCell。這類做法特別適合那些「單元格格式一致」的列表場景,例如團購列表、新聞列表等。
在控制器中實作自訂的代理、加載資料並負責表格刷新:
#import "SLViewController.h" #import "SLTgDatas.h" #import "SLTableViewCell.h" #import "SLFooterView.h" #import "SLHeaderView.h" @interface SLViewController ()@property (weak, nonatomic) IBOutlet UITableView *tableview; @property (nonatomic, strong) NSMutableArray *arrayM; @end @implementation SLViewController -(void)loadMoreData { NSLog(@"======="); SLTgDatas *da = [[SLTgDatas alloc] init]; da.title = @"西紅柿雞蛋"; da.price = @"12"; da.buyCount = @"56"; da.icon = @"2c97690e72365e38e3e2a95b934b8dd2"; [self.arrayM addObject:da]; [self.tableview reloadData]; } #pragma mark - 解析 plist 數據文件 -(NSMutableArray *)arrayM { if (_arrayM == nil) { NSString *fullpath = [[NSBundle mainBundle] pathForResource:@"tgs" ofType:@"plist"]; NSArray *array = [NSArray arrayWithContentsOfFile:fullpath]; NSMutableArray *arr = [NSMutableArray arrayWithCapacity:array.count]; for (NSDictionary *dict in array) { SLTgDatas *data = [SLTgDatas tgDataWithDiectionary:dict]; [arr addObject:data]; } _arrayM = arr; } return _arrayM; } - (void)viewDidLoad { [super viewDidLoad]; UINib *nib = [UINib nibWithNibName:@"SLFooterView" bundle:nil]; SLFooterView *footerview = [[nib instantiateWithOwner:nil options:nil] firstObject]; self.tableview.tableFooterView = footerview; footerview.delegate = self; SLHeaderView *headerview = [SLHeaderView headerWithView]; self.tableview.tableHeaderView = headerview; } #pragma mark - 填充數據顯示 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.arrayM.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { SLTableViewCell *cell = [SLTableViewCell cellWithTabelViewCell:tableView]; SLTgDatas *data = self.arrayM[indexPath.row]; cell.data = data; return cell; } #pragma mark - 設置狀態欄隱藏 -(BOOL)prefersStatusBarHidden { return YES; } @end
#import#import "SLGlobalCode.h" @interface SLTgDatas : NSObject @property (nonatomic, copy) NSString *title; @property (nonatomic, copy) NSString *icon; @property (nonatomic, copy) NSString *price; @property (nonatomic, copy) NSString *buyCount; @property (nonatomic, strong, readonly) UIImage *image; -(instancetype)initWithTgDirectionary:(NSDictionary *)dict; +(instancetype)tgDataWithDiectionary:(NSDictionary *)dict; @end
#import "SLTgDatas.h"
@interface SLTgDatas ()
{
UIImage *_image;
}
@end
@implementation SLTgDatas
-(UIImage *)image {
if (_image == nil) {
_image = [UIImage imageNamed:self.icon];
}
return _image;
}
// 抽取共用的初始化,方便其它地方複用
-(instancetype)initWithTgDirectionary:(NSDictionary *)dict {
if (self = [self init]) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+(instancetype)tgDataWithDiectionary:(NSDictionary *)dict {
return [[self alloc] initWithTgDirectionary:dict];
}
@end透過 xib 設計後,用連線把控制項關聯起來,方便控制器呼叫:
#import#import "SLTgDatas.h" @interface SLTableViewCell : UITableViewCell @property (nonatomic, strong) SLTgDatas *data; +(instancetype)cellWithTabelViewCell:(UITableView *)table; @end
#import "SLTableViewCell.h"
@interface SLTableViewCell ()
@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
@implementation SLTableViewCell
+(instancetype)cellWithTabelViewCell:(UITableView *)table {
static NSString *str = @"cell";
SLTableViewCell *cell = [table dequeueReusableCellWithIdentifier:str];
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"SLTgPlistView" owner:nil options:nil] firstObject];
}
return cell;
}
-(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#import@protocol SLFooterViewDelegate -(void)loadMoreData; @end @interface SLFooterView : UIView @property (nonatomic, weak) id delegate; @end
#import "SLFooterView.h"
@interface SLFooterView ()
@property (weak, nonatomic) IBOutlet UIButton *clieckbt;
@property (weak, nonatomic) IBOutlet UIView *jiazai;
@end
@implementation SLFooterView
- (IBAction)btnclick {
self.clieckbt.hidden = YES;
self.jiazai.hidden = NO;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)),
dispatch_get_main_queue(), ^{
if ([self.delegate respondsToSelector:@selector(loadMoreData)]) {
[self.delegate loadMoreData];
}
self.clieckbt.hidden = NO;
self.jiazai.hidden = YES;
});
}
@end以上就是用 xib 設計並顯示列表數據的完整流程。總結來說:自訂 UITableViewCell 有兩種常見方式,可依不同場景靈活選用。