新建一個(gè)繼承自UITableViewCell的子類,比如XMGTgCell
@interface XMGTgCell : UITableViewCell
@end
新建一個(gè)xib文件(文件名最好跟類名一致,比如XMGTgCell.xib)
- 綁定循環(huán)利用標(biāo)識(shí)
@interface XMGTgCell()
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *buyCountLabel;
@end
在XMGTgCell.h文件中提供一個(gè)模型屬性,比如XMGTg模型
@class XMGTg;
@interface XMGTgCell : UITableViewCell
/** 團(tuán)購模型數(shù)據(jù) */
@property (nonatomic, strong) XMGTg *tg;
@end
在XMGTgCell.m中重寫模型屬性的set方法
- 在set方法中給子控件設(shè)置模型數(shù)據(jù)
- (void)setTg:(XMGTg *)tg
{
_tg = tg;
// .......
}
在控制器中
[self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([XMGTgCell class]) bundle:nil] forCellReuseIdentifier:ID];
// 或者 因?yàn)閎undle:傳遞的參數(shù)為nil的話,默認(rèn)是mainBundle
[self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([XMGTgCell class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:ID];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 訪問緩存池
XMGTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 設(shè)置數(shù)據(jù)(傳遞模型數(shù)據(jù))
cell.tg = self.tgs[indexPath.row];
return cell;
}
#warning 默認(rèn)新建的xib里面的UITableCell的尺寸寬是320,高是44,當(dāng)初理解為會(huì)像新建的storyboard里面的正方形一樣,在里面開始布局,添加約束,因?yàn)槟莻€(gè)正方形的size是inferred類型,可推測的,然而UITableViewCell是默認(rèn)給的,這個(gè)不重要,就算你給了寬160,高40,它顯示的時(shí)候不是由自己決定的,而是由它的tableView來決定,高度由tableView.rowHeight來決定,寬度和tableView的寬度一樣的,所以,在那個(gè)320*44里面添加子控件并添加相應(yīng)的約束,沒有什么問題,它會(huì)默認(rèn)按照那個(gè)約束顯示的,over
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。