自定義cell

UItableViewCell 的自定義

在學(xué)習(xí)中檐薯,我們知道了cell的三種樣式凝赛,但是在實(shí)際編寫(xiě)中,很多時(shí)候我們需要用到不同的cell樣式坛缕,這時(shí)候我們就需要自定義cell墓猎。

  • 首先,我們需要在父視圖上新建一個(gè)tableView
UITableView * tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    //tableview的代理方法祷膳。所以要在上邊遵守它的協(xié)議陶衅。
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.rowHeight = 200.f;
    [self.view addSubview:tableView];

UITableViewDelegate,
UITableViewDataSource

  • 然后我們創(chuàng)建一個(gè)對(duì)象類(lèi),用于裝我們的對(duì)象的屬性直晨,

@property(nonatomic,retain)NSString *name;
@property(nonatomic,retain)NSString *gender;
@property(nonatomic,retain)NSString *phoneNumber;
@property(nonatomic,retain)NSString *contactImageName;

  • 然后我們需要在tableView的頁(yè)面引入頭視圖搀军,并且寫(xiě)一個(gè)獲取數(shù)據(jù)的方法,用于給對(duì)象賦值
-(void)getData {
    //創(chuàng)建聯(lián)系人對(duì)象勇皇,加到數(shù)組中
    Contact *contact1 = [[Contact alloc] init];
    contact1.name = @"小明";
    contact1.gender = @"男";
    contact1.phoneNumber = @"121212";
    contact1.contactImageName = @"1.jpg";
    
    Contact *contact2 = [[Contact alloc] init];
    contact2.name = @"小紅";
    contact2.gender = @"女";
    contact2.phoneNumber = @"121212123";
    contact2.contactImageName = @"3.jpg";
    
    self.contactArray = @[contact1,contact2];
}

*不要忘了tableView的兩個(gè)必須實(shí)現(xiàn)的代理方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.contactArray.count;  
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //從數(shù)組取出的對(duì)象
    Contact *contact = self.contactArray[indexPath.row];
    //static 只初始化一次
    static NSString *contactIdentifier = @"contact";
    //用我們自定義的cell罩句,
    ContactCell *cell = [tableView dequeueReusableCellWithIdentifier:contactIdentifier];
    if (nil == cell) {
        cell = [[ContactCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:contactIdentifier];
    }
    //左邊的contact 是cell中的,右邊的是從數(shù)組中取出的聯(lián)系人對(duì)象
    cell.contact = contact;
    
    return cell;
}

  • 接下來(lái)是自定義cell的部分敛摘,我們需要新建一個(gè)cell類(lèi)门烂,繼承于UItableViewCell。
  • 定義要顯示在cell上的控件的屬性,

@interface ContactCell ()
@property(nonatomic,retain)UIImageView * contactImageView;
@property(nonatomic,retain)UILabel * nameLable;
@property(nonatomic,retain)UILabel * genderLable;
@property(nonatomic,retain)UILabel * phoneNumberLable;
@end

  • 然后是重寫(xiě)cell的方法:
//重寫(xiě)cell的方法
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

        self.contactImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.contactImageView.backgroundColor = [UIColor redColor];
        //contentView是cell自帶的view屯远,
        [self.contentView addSubview:self.contactImageView];
        
        self.nameLable = [[UILabel alloc] initWithFrame:CGRectZero];
        self.nameLable.backgroundColor = [UIColor grayColor];
        [self.contentView addSubview:self.nameLable];
        
        self.genderLable = [[UILabel alloc] initWithFrame:CGRectZero];
        self.genderLable.backgroundColor = [UIColor blueColor];
        [self.contentView addSubview:self.genderLable];
        
        self.phoneNumberLable = [[UILabel alloc] initWithFrame:CGRectZero];
        self.phoneNumberLable.backgroundColor = [UIColor orangeColor];
        [self.contentView addSubview:self.phoneNumberLable];
        
    }
    return self;
}

  • cell的布局蔓姚,防止以后我們需要進(jìn)行改動(dòng),我們可以把cell上控件的布局寫(xiě)在下面的方法里;
//在這個(gè)方法寫(xiě)子視圖的布局代碼
-(void)layoutSubviews {
    //調(diào)用父類(lèi)的方法
    [super layoutSubviews];
    self.contactImageView.frame = CGRectMake(10, 10, 100, 120);
    
    self.nameLable.frame = CGRectMake(self.contactImageView.frame.origin.x + self.contactImageView.frame.size.width + 10, self.contactImageView.frame.origin.y, 100, 30);
    
    CGRect genderFrame = self.nameLable.frame;
    //調(diào)整genderFrame的y值
    genderFrame.origin.y = self.nameLable.frame.origin.y + self.nameLable.frame.size.height + 5;

    //最后把調(diào)整好的frame賦給genderLable的frame
    self.genderLable.frame = genderFrame;
    
    CGRect phoneNumberFrame = genderFrame;
    phoneNumberFrame.origin.y = genderFrame.origin.y + genderFrame.size.height + 5;
    self.phoneNumberLable.frame = phoneNumberFrame;
}
  • 在賦值的時(shí)候慨丐,我們需要重寫(xiě)setter方法
//重寫(xiě)setter方法坡脐。
-(void)setContact:(Contact *)contact {
    if (_contact != contact) {
        _contact = contact;
        //給視圖賦值,
        self.nameLable.text = contact.name;
        self.genderLable.text = contact.gender;
        self.phoneNumberLable.text = contact.phoneNumber;
        self.contactImageView.image = [UIImage imageNamed:contact.contactImageName];
    }
    //注意:這里不能寫(xiě)成 self.contact = contact;  因?yàn)檫@樣會(huì)無(wú)限的調(diào)用自己房揭,叫做遞歸备闲;
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市捅暴,隨后出現(xiàn)的幾起案子恬砂,更是在濱河造成了極大的恐慌,老刑警劉巖蓬痒,帶你破解...
    沈念sama閱讀 217,657評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件泻骤,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡乳幸,警方通過(guò)查閱死者的電腦和手機(jī)瞪讼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)粹断,“玉大人符欠,你說(shuō)我怎么就攤上這事∑柯瘢” “怎么了希柿?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,057評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)养筒。 經(jīng)常有香客問(wèn)我曾撤,道長(zhǎng),這世上最難降的妖魔是什么晕粪? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,509評(píng)論 1 293
  • 正文 為了忘掉前任挤悉,我火速辦了婚禮,結(jié)果婚禮上巫湘,老公的妹妹穿的比我還像新娘装悲。我一直安慰自己,他們只是感情好尚氛,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,562評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布诀诊。 她就那樣靜靜地躺著,像睡著了一般阅嘶。 火紅的嫁衣襯著肌膚如雪属瓣。 梳的紋絲不亂的頭發(fā)上载迄,一...
    開(kāi)封第一講書(shū)人閱讀 51,443評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音抡蛙,去河邊找鬼护昧。 笑死,一個(gè)胖子當(dāng)著我的面吹牛溜畅,可吹牛的內(nèi)容都是我干的捏卓。 我是一名探鬼主播极祸,決...
    沈念sama閱讀 40,251評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼慈格,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了遥金?” 一聲冷哼從身側(cè)響起浴捆,我...
    開(kāi)封第一講書(shū)人閱讀 39,129評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎稿械,沒(méi)想到半個(gè)月后选泻,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,561評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡美莫,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,779評(píng)論 3 335
  • 正文 我和宋清朗相戀三年页眯,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片厢呵。...
    茶點(diǎn)故事閱讀 39,902評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡窝撵,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出襟铭,到底是詐尸還是另有隱情碌奉,我是刑警寧澤,帶...
    沈念sama閱讀 35,621評(píng)論 5 345
  • 正文 年R本政府宣布寒砖,位于F島的核電站赐劣,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏哩都。R本人自食惡果不足惜魁兼,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,220評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望漠嵌。 院中可真熱鬧咐汞,春花似錦、人聲如沸献雅。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,838評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)挺身。三九已至侯谁,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背墙贱。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,971評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工热芹, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人惨撇。 一個(gè)月前我還...
    沈念sama閱讀 48,025評(píng)論 2 370
  • 正文 我出身青樓伊脓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親魁衙。 傳聞我的和親對(duì)象是個(gè)殘疾皇子报腔,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,843評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容