實現(xiàn)團購:自定義Cell
- 通過xib實現(xiàn)自定義Cell
每個cell中的內容是固定的,控件個數(shù)、位置、尺寸等都是一樣的時候(團購案例)
- 完全手寫,實現(xiàn)自定義cell
每個cell中的結構不一樣,控件個數(shù)、延時等都是不一樣的時候(微博案例)
系統(tǒng)自帶的cell不好用,那么就是用自定義Cell。
MVC方式組織項目結構:group分組。
Models,Views,Controllers,Others,Supporting Files
使用自定義單元格。使用xib重定義cell
新建xib:new--iOS--UserInterface--Empty--CZGoodsCell.xib
往xib中,拖拽1個UITableViewCell控件。設置高度44.
往里面拖圖片框:高度寬度40,x=5,y=2
添加Label,用于顯示名稱:
添加Label,用于顯示價格:
添加Label,顯示已購買的數(shù)量。
自己寫一個類CZGoodsCell:繼承自 UITableViewCell,將該類CZGoodsCell設置到Xib的Class屬性中。
給xib的cell的控件拖線: 到 CZGoodsCell.m中。
// CXGoodsCell.h
#import <UIKit/UIKit.h>
@interface CXGoodsCell: UITableViewCell
@property(nonatomic, strong) CZGoods *goods;
+ (instancetype)goodsCellWithTableView:(UITableView *)tableView; // 封裝1個創(chuàng)建自定義cell的方法。
@end
#import "CZGoodsCell.h"
@interface CZGoodsCell()
// 4個控件拖線
@property(weak, nonatomic) IBOutlet UIImageView *imgViewIcon;
@property(weak, nonatomic) IBOutlet UILabel *lblTitle;
@property(weak, nonatomic) IBOutlet UILabel *lblPrice;
@property(weak, nonatomic) IBOutlet UILabel *lblBuyCount;
@end
@implementation CZGoodsCell
+ (instancetype)goodsCellWithTableView:(UITableView *)tableView
{
static NSString *ID = @"goods_cell";
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:ID];
if(cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"CZGoodsCell" owner:nil options:nil] firstObject];
// 選中xib文件,屬性中Identifier中設置可重用id的名稱:goods_cell,給xib指定可重用id。
}
return cell;
}
// 重寫goods屬性的setter方法,給數(shù)據(jù)賦值。
- (void)setGoods: (CZGoods *) goods
{
_goods = goods;
// 把模型數(shù)據(jù),設置給子控件。
self.imgViewIcon.image = [UIImage imageNamed:goods.icon];
self.lblTitle.text = goods.title;
self.lblPrice.text = [NSString stringWithFormat:@"¥ %@",goods.price];
self.lblBuyCount.text = [NSString stringWithFormat:@"%@ 人已購買",goods.buyCount];
}
@end
// 通過xib的方式來創(chuàng)建單元格
-(UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
// 1.獲取數(shù)據(jù)模型
CZGoods *model = self.goods[indexPath.row];
// 2. 創(chuàng)建單元格:使用xib的方式來創(chuàng)建單元格。
/* static NSString *ID = @"goods_cell";
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:ID];
if(cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"CZGoodsCell" owner:nil options:nil] firstObject];
// 選中xib文件,屬性中Identifier中設置可重用id的名稱:goods_cell,給xib指定可重用id。
} */
// 2.2 封裝到類方法中。
CZGoodsCell *cell = [CZGoodsCell goodsCellWithTableView:tableView];
// 3.把模型數(shù)據(jù)設置給單元格。
cell.goods = model;
// 4.返回單元格
return cell;
}
-(void)viewDidLoad {
[super viewDidLoad];
self.tableView.rowHeight = 44;
}
cell.imgView.image = model.icon;
給cell的屬性賦值。
1).封裝不夠完整,每次用到了都要寫設置數(shù)據(jù)代碼。為屬性賦值。
2).控制器太強依賴于單元格cell,一旦cell子控件屬性修改了,控制器也要改。造成緊耦合。
cell.goods = model;
在自定義cell的set方法中,給各個屬性賦值。
setGoods:(CZGoodsCell *)model
{
self.imgView.image = model.icon;
self.lblTitle.text = model.title;
}
其他:
self.tableView.rowHeight = 44;
如果沒有給tableView指定行高,運行起來會報警告。
單元格xib,子控件是添加到Content View中,不是直接放在單元格中的,是放在單元格的Content View里面。
2023/05/30 周二