UITableViewCell總結(jié)

// 注意:
1. 通過(guò)xib來(lái)創(chuàng)建UITableViewCell需要在xib的identity上寫(xiě)上重用標(biāo)識(shí)

 2. 通過(guò)storyboard來(lái)創(chuàng)建UITableViewCell需要在storyboard的identity上寫(xiě)上重用標(biāo)識(shí)

 3.  有時(shí)候每一行的cell用的不一定是同一種UITableVIewCell,對(duì)象池中也會(huì)有不同的UITableViewCell等待重用,那么在重用的時(shí)候可能得到的是錯(cuò)誤的UITableViewCell}
     解決方法:可以通過(guò)重用屬性reuseIdentifier來(lái)對(duì)應(yīng)不同的UITableViewCell

 4. 往cell添加子控件的時(shí)候,把子控件添加到contentView上面

 5. 不應(yīng)該在initWithStyle中設(shè)置子控件的Frame,因?yàn)楦缚丶腇rame還沒(méi)有確定,子控件設(shè)置Frame不太好,我們應(yīng)該在layoutSubviews中設(shè)置Frame
 layoutSubviews (一定要調(diào)用父類(lèi)) :
    當(dāng)一個(gè)控件的frame發(fā)生改變的時(shí)候調(diào)用,Frame前后要不一致
    將一個(gè)控件添加到當(dāng)前控件的時(shí)候調(diào)用

一、UITableViewCell重用的封裝

+ (instancetype)HeroCell:(UITableView *)tableView;{
    HeroTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
    if (!cell) {
        cell = [[HeroTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"ID"];
    }
    return cell;
}

二、UITableViewCell的封裝 - 系統(tǒng)自定義

#import <UIKit/UIKit.h>
@class Hero;
@interface HeroTableViewCell : UITableViewCell
+ (instancetype)HeroCell;
@property (nonatomic, strong) Hero *hero;
@end
#import "HeroTableViewCell.h"
#import "Hero.h"
@implementation HeroTableViewCell
+ (instancetype)HeroCell{
    HeroTableViewCell *cell = [[HeroTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    return cell;
}

- (void)setHero:(Hero *)hero{
    _hero = hero;
    self.imageView.image = [UIImage imageNamed:hero.icon];
    self.textLabel.text = hero.name;
    self.detailTextLabel.text = hero.intro;
}
@end

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    Hero *hero = self.heros[indexPath.row];
    HeroTableViewCell *cell = [HeroTableViewCell HeroCell];
    cell.hero = hero;
    return cell;
}

三、UITableViewCell的封裝 - 動(dòng)態(tài)單元格
1.動(dòng)態(tài)單元格得使用UITableViewController

2.UITableViewController的動(dòng)態(tài)單元格cell要寫(xiě)自定義的UITableViewCell,從而通過(guò)UITableViewCell實(shí)現(xiàn)自定義

3.UITableViewCell -> AppViewCell的代碼實(shí)現(xiàn)

#import <UIKit/UIKit.h>
@class App;
@class AppViewCell;
/// 代理
@protocol AppViewCellDelegate <NSObject>
@optional
- (void)downloadBtnDidClick:(AppViewCell *)appViewCell;
@end

@interface AppViewCell : UITableViewCell
+ (instancetype) appViewCell:(UITableView *)tableView;
@property (nonatomic, strong) App *app;
/// 代理屬性
@property (nonatomic, assign) id<AppViewCellDelegate> delegate;
@end
#import "AppViewCell.h"
#import "App.h"

@interface AppViewCell ()
/// 屬性連線(xiàn)
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *nameView;
@property (weak, nonatomic) IBOutlet UILabel *detailView;
@property (weak, nonatomic) IBOutlet UIButton *downloadBtn;
@end

@implementation AppViewCell
- (IBAction)downloadBtnDidClick {
    /// 禁用下載按鈕
    self.downloadBtn.enabled = NO;
    /// 設(shè)置已下載
    self.app.isDownloaded = YES;
    /// 判斷代理是否實(shí)現(xiàn)了代理方法
    if ([self.delegate respondsToSelector:@selector(downloadBtnDidClick:)]) {
        [self.delegate downloadBtnDidClick:self];
    }
}

+ (instancetype)appViewCell:(UITableView *)tableView{
    //dequeueReusableCellWithIdentifier會(huì)從緩存中創(chuàng)建Cell,但是如果緩存中沒(méi)有可重用的cell,則查詢(xún)系統(tǒng)的cell原型,通過(guò)原型創(chuàng)建cell,.沒(méi)有原型則報(bào)錯(cuò):
    AppViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"app"];
    return cell;
}

- (void)setApp:(App *)app{
    _app = app;
    self.iconView.image = [UIImage imageNamed:app.icon];
    self.nameView.text = app.name;
    self.detailView.text=[NSString stringWithFormat:@"大?。?@M,下載數(shù)量:%@",app.size,app.download];
    self.downloadBtn.enabled = !app.isDownloaded;
}
@end

四、UITableViewCell的封裝 - xib
1.xib名和UITableViewCell名一般相同
2.xib中的控件要進(jìn)行屬性連線(xiàn)
3.代碼實(shí)現(xiàn)

#import <UIKit/UIKit.h>
@class CZTg;

@interface CZTgCell : UITableViewCell
//返回當(dāng)前自定義的cell
+ (instancetype) tgCell:(UITableView *)tableView;
//為當(dāng)前自定義cell賦值
@property (nonatomic,strong) CZTg *tg;

@end
#import "CZTgCell.h"
#import "CZTg.h"

@interface CZTgCell()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *titleView;
@property (weak, nonatomic) IBOutlet UILabel *priceView;
@property (weak, nonatomic) IBOutlet UILabel *buyCountView;
@end
@implementation CZTgCell

//創(chuàng)建自定義cell
+ (instancetype)tgCell:(UITableView *)tableView
{
    //1.定義重用標(biāo)識(shí)
    NSString *ID=@"tg";
    //2.從緩存中創(chuàng)建cell
    CZTgCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
    //3.判斷是否成功創(chuàng)建出cell,如果沒(méi)有就自己從Xib中創(chuàng)建
    if (cell==nil) {
        NSLog(@"aaaaaaa");
        cell=[[[NSBundle mainBundle] loadNibNamed:@"CZTgCell" owner:self options:nil] lastObject];
    }
    return  cell;
}

- (void)setTg:(CZTg *)tg
{
    _tg=tg;
    self.iconView.image=[UIImage imageNamed:tg.icon];
    self.titleView.text=tg.title;
    self.priceView.text=[NSString stringWithFormat:@"¥%@",tg.price];
    self.buyCountView.text=[NSString stringWithFormat:@"%@人已購(gòu)買(mǎi)",tg.buyCount];
}
@end

五、UITableViewCell的封裝 - 純代碼
1.思路:
1.1重寫(xiě)UITableViewCell的initWithStyle方法(記得先super父類(lèi)方法),在方法中新建控件并添加到cell中,再設(shè)置控件的屬性值
1.2在-(void)setMessageFrame方法中設(shè)置數(shù)據(jù)
1.3在-(void)layoutSubviews中設(shè)置Frame (layoutSubviews是在子控件添加到父類(lèi)的時(shí)候會(huì)調(diào)用)
2.控件的Frame已經(jīng)在模型中實(shí)現(xiàn)了,所以cell就不需要再設(shè)置Frame
3.代碼的實(shí)現(xiàn):

#import <UIKit/UIKit.h>
@class CZMessageCellFrame;

@interface CZMessageCell : UITableViewCell
+ (instancetype) messageCell:(UITableView *)tableView;

@property (nonatomic,strong) CZMessageCellFrame *messageFrame;
@end
#import "CZMessageCell.h"
#import "CZMessage.h"
#import "CZMessageCellFrame.h"

@interface CZMessageCell ()
@property (nonatomic,weak) UILabel *timeView;
@property (nonatomic,weak) UIImageView *iconView;
@property (nonatomic,weak) UIButton *textView;
@end

@implementation CZMessageCell

+ (instancetype) messageCell:(UITableView *)tableView
{
    NSString *ID=@"QQ";
    CZMessageCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
    if (cell==nil) {
        //這個(gè)方法是父類(lèi)的方法,也就意味著它只能創(chuàng)建出父類(lèi)中默認(rèn)的系統(tǒng)自帶的cell.而不能滿(mǎn)足我們的需要,所以我們就需要做重寫(xiě),在調(diào)用方法的時(shí)候調(diào)用我們子類(lèi)自己重寫(xiě)過(guò)后的方法。
        cell=[[CZMessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return  cell;
}
//根據(jù)多態(tài),系統(tǒng)會(huì)根據(jù)類(lèi)型調(diào)用子類(lèi)重寫(xiě)過(guò)后的方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    //1.先初始化父類(lèi)成員
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //1.添加timeView
        UILabel *timeView=[[UILabel alloc] init];
        self.timeView=timeView;
        //設(shè)置居中
        timeView.textAlignment=NSTextAlignmentCenter;
        timeView.font=[UIFont systemFontOfSize:13];
        timeView.textColor=[UIColor lightGrayColor];
        [self addSubview:timeView];
        //2.添加iconView
        UIImageView *iconView=[[UIImageView alloc] init];
        self.iconView=iconView;
        [self addSubview:iconView];
        //3.添加textView
        UIButton *textView=[[UIButton alloc] init];
        self.textView=textView;
        //設(shè)置按鈕的文本色
        [textView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //設(shè)置按鈕的字體大小
        textView.titleLabel.font=[UIFont systemFontOfSize:14];
        //設(shè)置自動(dòng)換行
        textView.titleLabel.numberOfLines=0;
        //為按鈕添加內(nèi)容的內(nèi)間距
        textView.contentEdgeInsets=UIEdgeInsetsMake(0, 20, 0, 20);
        //為按鈕添加背景色
        //textView.backgroundColor=[UIColor redColor];
        [self addSubview:textView];
    }
    return self;
}

- (void)setMessageFrame:(CZMessageCellFrame *)messageFrame
{
    _messageFrame=messageFrame;
    //設(shè)置數(shù)據(jù)
    [self setDatas];
}
//設(shè)置數(shù)據(jù)
- (void) setDatas
{
    CZMessage *msg=self.messageFrame.message;

    self.timeView.text=msg.time;

    if (msg.type==MessageTypeMe) {
        self.iconView.image=[UIImage imageNamed:@"me"];
        //根據(jù)消息類(lèi)型設(shè)置消息文本的背景圖片
        [self.textView setBackgroundImage:[self resizeImageWithName:@"chat_send_nor"] forState:UIControlStateNormal];
        [self.textView setBackgroundImage:[self resizeImageWithName:@"chat_send_press_pic"] forState:UIControlStateHighlighted];
    }
    else
    {
        self.iconView.image=[UIImage imageNamed:@"other"];
        [self.textView setBackgroundImage:[self resizeImageWithName:@"chat_recive_nor"] forState:UIControlStateNormal];
        [self.textView setBackgroundImage:[self resizeImageWithName:@"chat_recive_press_pic"] forState:UIControlStateHighlighted];
    }
    [self.textView setTitle:msg.text forState:UIControlStateNormal];

}

//返回拉伸之后的圖片
- (UIImage *) resizeImageWithName:(NSString *)name
{
    return [[UIImage imageNamed:name] resizableImageWithCapInsets:UIEdgeInsetsMake(30, 20, 18, 30) resizingMode:UIImageResizingModeStretch];
}

//當(dāng)將當(dāng)前控件添加到父容器的時(shí)候,自動(dòng)調(diào)用這個(gè)方法,設(shè)置它的子控件的Frame
//如現(xiàn)在這個(gè)情況:當(dāng)創(chuàng)建好cell,添加到tableView中的時(shí)候,會(huì)調(diào)用這個(gè)方法設(shè)置cell 的三個(gè)子控件的frame
- (void) layoutSubviews
{
    //判斷是否需要顯示時(shí)間
    self.timeView.hidden=! self.messageFrame.showTime;
    self.timeView.frame=self.messageFrame.timeViewF;
    self.iconView.frame=self.messageFrame.iconViewF;
    self.textView.frame=self.messageFrame.textViewF;
}
@end
最后編輯于
?著作權(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),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,018評(píng)論 4 61
  • UITableViewCell 父類(lèi)是UIView UITableView的每一行都是一個(gè)UITableViewC...
    翻這個(gè)墻閱讀 6,796評(píng)論 0 1
  • 1.屬性readwrite,readonly,assign,retain,copy,nonatomic 各是什么作...
    曾令偉閱讀 1,127評(píng)論 0 10
  • 一大早朋友打電話(huà)送來(lái)幾張票:“今晚七點(diǎn),來(lái)看我們縣的春晚!” 盡管不是演員,到現(xiàn)場(chǎng)的那一刻也是無(wú)比激動(dòng)的:絢麗的舞...
    時(shí)光呵閱讀 175評(píng)論 0 0
  • 半夜 生物鐘把我鬧醒 床頭迷籠的光 黑夜里充盈的瞳眸 母親這疲累卑小的身份 時(shí)常驕傲里填滿(mǎn)喪氣 時(shí)常心安里澆灌惶恐...
    半壁殘?jiān)?/span>閱讀 361評(píng)論 2 4

友情鏈接更多精彩內(nèi)容