tabelViewCell

UITableViewCell:

1.使用系統(tǒng)自定義的各種UITableViewCell的樣式

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString* indentifier = @"cell";

MyTableCell* cell = [tableView dequeueReusableCellWithIdentifier:indentifier];

if (!cell) {

cell = [[[MyTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier]autorelease];

}

cell.textLabel.text = [_data objectAtIndex:indexPath.row];

cell.detailTextLabel.text = @"detail";

cell.imageView.image = [UIImage imageNamed:@"checkmark.png"];

return cell;

}

使用UITableViewCellStyleDefault的效果:

使用UITableViewCellStyleValue1的效果:

使用UITableViewCellStyleValue2的效果:

在UITableViewCell內(nèi)默認(rèn)是有contentview和accessoryView這兩個(gè)subview的,contentview中的subview根據(jù)不同的cell的style會(huì)使用不同的布局。contentview和其中的默認(rèn)subview會(huì)根據(jù)cell的編輯狀態(tài)出現(xiàn)的控件自動(dòng)縮進(jìn),自定義cell時(shí)可以把自定義控件添加在contentview中,也可以直接添加到cell中。

2.設(shè)置UITableViewCell的屬性

//cell的右邊輔助按鈕的樣式

cell.accessoryType = UITableViewCellAccessoryCheckmark;

//自定義cell右邊的輔助按鈕

cell.accessoryView = nil;

//自定義cell的背景

cell.backgroundView = nil;

//設(shè)置cell的contentview中的detail的文字內(nèi)容

cell.detailTextLabel.text = @"";

//查看cell當(dāng)前的編輯模式

int style = cell.editingStyle;

//設(shè)置當(dāng)cell進(jìn)入編輯模式時(shí)的輔助按鈕樣式

cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;

//自定義cell進(jìn)入編輯模式后輔助按鈕

cell.editingAccessoryView = nil;

//獲取cell的縮進(jìn)級(jí)別

int level = cell.indentationLevel;

//獲取cell的縮進(jìn)寬度

float width = cell.indentationWidth;

//設(shè)置cell被選中時(shí)的背景

cell.selectedBackgroundView = nil;

//設(shè)置cell的選中狀態(tài)樣式

cell.selectionStyle = UITableViewCellSelectionStyleBlue;

//設(shè)置cell的contentview中的textlabel文字內(nèi)容

cell.textLabel.text = @"";

3.自定義的UITableViewCell重寫父類的方法

//初始化uitableviewcell后,自定義cell添加subview

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

//當(dāng)cell被選中時(shí),uitableview內(nèi)部會(huì)自動(dòng)調(diào)用該方法,重寫該方法可以在cell被選中時(shí)做一些額外的操作

- (void)setSelected:(BOOL)selected animated:(BOOL)animated

//當(dāng)cell處于高亮狀態(tài)時(shí),uitableview內(nèi)部會(huì)自動(dòng)調(diào)用該方法,重寫該方法可以在cell處于高亮?xí)r做一些額外操作

-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated

//重寫layoutsubviews方法,為了查看當(dāng)cell改變編輯狀態(tài)時(shí),有什么subview

-(void)layoutSubviews{

[super layoutSubviews];

NSArray* subs = self.subviews;

for (UIView* sub in subs) {

NSLog(@"view:%@",sub);

}

}

當(dāng)進(jìn)入刪除編輯模式時(shí),cell的subview有一個(gè)叫UITableViewCellDeleteConfirmationControl的subview,這代表刪除按鈕??梢孕薷脑搗iew達(dá)到修改刪除按鈕的位置,大小等屬性。

當(dāng)進(jìn)入移動(dòng)編輯模式時(shí),cell的subview有一個(gè)叫UITableViewCellReorderControl的subview,這個(gè)代表移動(dòng)按鈕。可以修改該view達(dá)到修改移動(dòng)按鈕的位置,大小等屬性。

當(dāng)進(jìn)入插入編輯模式時(shí),cell的subview有一個(gè)叫UITableViewCellEditControl的subview,這個(gè)代表添加按鈕??梢孕薷脑搗iew達(dá)到修改添加按鈕的位置,大小等屬性。

//當(dāng)cell的狀態(tài)變?yōu)榫庉嫊r(shí),uitableview內(nèi)部會(huì)自動(dòng)調(diào)用該方法,重寫該方法可以改變cell的布局

-(void)willTransitionToState:(UITableViewCellStateMask)state{

[super willTransitionToState:state];

}

//當(dāng)cell的狀態(tài)變?yōu)榫庉嫊r(shí),uitableview內(nèi)部會(huì)自動(dòng)調(diào)用該方法,重寫該方法可以改變cell的布局

-(void)didTransitionToState:(UITableViewCellStateMask)state{

[super didTransitionToState:state];

//滑動(dòng)出現(xiàn)的刪除按鈕state是2的,編輯狀態(tài)下的刪除按鈕state是3的

if (state == UITableViewCellStateShowingDeleteConfirmationMask||state==3) {

for (UIView *subview in self.subviews) {

//cell的subview為UITableViewCellDeleteConfirmationControl時(shí),代表是刪除按鈕

if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) {

UIView *deleteButtonView = subview;

CGRect f = deleteButtonView.frame;

f.origin.x -= 50;

deleteButtonView.frame = f;}

}

}

//插入和移動(dòng)的編輯狀態(tài)state都是1

else if(state==UITableViewCellStateShowingEditControlMask){

for (UIView *subview in self.subviews) {

NSString* type = @"";

//判斷如果cell當(dāng)前是插入模式,則尋找UITableViewCellEditControl的subview,代表添加按鈕

if (self.editingStyle==UITableViewCellEditingStyleInsert) {

type = @"UITableViewCellEditControl";

}

//否則尋找UITableViewCellReorderControl的subview,代表移動(dòng)按鈕

else type = @"UITableViewCellReorderControl";

if ([NSStringFromClass([subview class]) isEqualToString:type]) {

UIView *deleteButtonView = [subview.subviews objectAtIndex:0];

CGRect f = deleteButtonView.frame;

f.origin.x -= 50;

deleteButtonView.frame = f;

}

}

}

}

最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件,我們平時(shí)使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,282評(píng)論 3 38
  • UITableViewCell使我們經(jīng)常使用的,在開發(fā)中我們經(jīng)常會(huì)通過自定義UITableViewCell來實(shí)現(xiàn)各...
    魔性佛心閱讀 1,631評(píng)論 1 2
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,019評(píng)論 4 61
  • (3)人體自身有機(jī)物構(gòu)成所需元素來源是空氣 氮平衡理論指出,人吃入食物中的氮元素總氮平衡,即吃進(jìn)多少,排除多少,沒...
    道易無成2閱讀 344評(píng)論 0 0
  • 看過一篇文章《我為什么不讓女兒遠(yuǎn)嫁?》,文章大概是這么說的: 遠(yuǎn)嫁,對(duì)于一個(gè)母親來說,太舍不得了;對(duì)于一個(gè)女人來說...
    sweet許許閱讀 472評(píng)論 0 0

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