標籤:style color 使用 strong width 檔案
在實際做項目中,使用系統內建的tableView時,cell的樣式單一,不易改變。而使用xib時,能改變cell的樣式,但是項目不具有可改性,xib 一旦創立,內容不會改變,這裡,利用封裝的思想,用純程式碼建立自訂cell,資料使用plist檔案儲存體
1、首先建立一個資料模型(moreBang),在標頭檔中(moreBang.h)定義模型中的屬性,並將字典轉為模型資料:
在moreBang.m中實現方法:(在對象方法中還可以使用KVC轉化)
2、其次,在建立cell的frame模型,設定各個空間的位置,建立moreBangFrame.h,並將資料模型傳入
在moreBangFrame.m檔案中設定frame:
3、建立moreBangCell,繼承UITableViewCell,moreBangCell.h的設定如下
#import <UIKit/UIKit.h>
@class moreBang,moreBangFrame;
@interface moreBangCell : UITableViewCell
@property(nonatomic,strong) moreBangFrame * morebangframe;
// 重寫cell方法
+(instancetype)cellWithTableView:(UITableView *)tableView;
@end
在moreBangCell.m檔案中對模型資料進行設定
#import "moreBangCell.h"
#import "moreBang.h"
#import "moreBangFrame.h"
#import "UIImage+HL.h"
@interface moreBangCell()
@property(nonatomic,weak) UIImageView * iconView;
@property(nonatomic,weak) UILabel * nameView;
@property(nonatomic,weak) UILabel * textView;
@end
@implementation moreBangCell
+(instancetype)cellWithTableView:(UITableView *)tableView
{
// 1、建立cell
static NSString *ID = @"Cell";
moreBangCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell ==nil) {
cell = [[moreBangCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
// 在這個方法中添加cell的子控制項
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
if (self) {
// 頭像
UIImageView * iconView = [[UIImageView alloc]init];
[self.contentView addSubview:iconView];
self.iconView = iconView;
// 暱稱
UILabel * nameView = [[UILabel alloc]init];
nameView.font = [UIFont systemFontOfSize:12];
[self.contentView addSubview:nameView];
self.nameView = nameView;
// 本文
UILabel * textView = [[UILabel alloc]init];
textView.numberOfLines = 0;
textView.font = [UIFont systemFontOfSize:12];
[self.contentView addSubview:textView];
self.textView = textView;
}
return self;
}
-(void)setMorebangframe:(moreBangFrame *)morebangframe
{
_morebangframe = morebangframe;
//設定子控制項的frame
[self settingFrame];
//給子控制項賦值資料
[self settingData];
}
/**
* 設定資料
*/
-(void)settingData
{
// 映像資料
self.iconView.image = [UIImage imageWithNamed:_morebangframe.morebang.icon];
// 暱稱
self.nameView.text = _morebangframe.morebang.name;
// 本文
self.textView.text = _morebangframe.morebang.text;
}
-(void)settingFrame
{
// 設定映像
self.iconView.frame = _morebangframe.iconF;
self.nameView.frame = _morebangframe.nameF;
// 設定本文
self.textView.frame = _morebangframe.textF;
}
@end
4、在控制器中匯入所需的標頭檔,定義一個數組:
@property(nonatomic,strong) NSArray * myMorebangs;
載入資料庫,懶載入:
將轉換好的資料對應給每行cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
moreBangCell *cell = [moreBangCell cellWithTableView:tableView];
cell.morebangframe = self.myMorebangs[indexPath.row];
// 返回cell
return cell;
}
#pragma mark -設定每組的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 擷取當前索引的frame
moreBangFrame * moreBangFrame = self.myMorebangs[indexPath.row];
// 返回美航的行高
return moreBangFrame.cellHeight;
}