UITableView

自定義單元格

表格無論有多少中自定義單元格樣式 每一種自定義單元格都有復用的能力
所以每一個單元格都要帶有一個靜態(tài)局部變量作為唯一標識

自定義單元格純手寫的

需要在自定義單元格類中重寫-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier方法,并且把所有要創(chuàng)建的控件定義在其中

方式一:直接在cellForRowAtIndexPath中實現(xiàn)并判斷

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * string = @"custom";
    FirstTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (cell == nil) {
    cell = [[FirstTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:string];
    }
    cell.nameLabel.text = dataSource[indexPath.row][@"personName"];
    cell.phoneLabel.text = dataSource[indexPath.row][@"phoneNum"];
    return cell;
    }

方式二:在創(chuàng)建表格/集合視圖的時候調(diào)用registerClass: forCellWithReuseIdentifier:

方法,且在cellForRowAtIndexPath方法中不用判斷為空,可直接重用/賦值

自定義單元格帶有XIB文件的

//為表格添加單元格的方法有兩種:
//<1>先注冊單元格文件
[table registerNib:[UINib nibWithNibName:@"CustomTableViewCell" bundle:nil] forCellReuseIdentifier:@"string"];
此方法在你創(chuàng)建UITableView的時候使用
可以不設置xib上的indentifier屬性值
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//單元格的標識上下必須完全相同
CustomTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"string"];
//此處不需要判斷單元格是否為空因為上面的viewDidLoad方法中已經(jīng)注冊過單元格了 所以一定不會為空
cell.nameLabel.text = dataSource[indexPath.row][@"personName"];
cell.phoneLabel.text = dataSource[indexPath.row][@"phoneNum"];
return cell;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//<2>使用XIB定義單元格的第二種方法
static NSString * string = @"string";
//[注意]此處的靜態(tài)局部變量的內(nèi)容一定要和XIB文件中單元格的identifier屬性的內(nèi)容完全相同 使得cell能夠重用 否則單元格會不斷的創(chuàng)建 最終導致內(nèi)存溢出/程序崩潰
CustomTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:string];
if(cell == nil)
{
cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:self options:nil] lastObject];
}
cell.picImageView.image = [UIImage imageNamed:dataSource[indexPath.row][@"imageName"]];
cell.titleLabel.text = dataSource[indexPath.row][@"title"];
cell.contentLabel.text = dataSource[indexPath.row][@"content"];
cell.collectionLabel.text = dataSource[indexPath.row][@"collection"];
return cell;
}

擴展

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]
如果需要使用這個方法,你必須使用配套的方法來一起用,下面兩個配套方法選其一:

  • (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(5_0);
  • (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
    上述兩個方法在你創(chuàng)建UITableView的時候調(diào)用

tableview使用

tableview刷新,移動到最后一行

[_tableView reloadData];
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:_dataSource.count-1 inSection:0];
[_tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
不斷改變edit的編輯狀態(tài)
[_tableView setEditing:editing animated:YES];

確定cell的編輯模式

  • (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
    return UITableViewCellEditingStyleDelete;
    }else
    return UITableViewCellEditingStyleInsert;
    }

//提交編輯的結(jié)果

  • (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if(editingStyle == UITableViewCellEditingStyleDelete){
    //先刪除數(shù)據(jù)源部分(先刪除某一行)
    //找到對應的組 再刪除指定行
    [_dataArray [indexPath.section]removeObjectAtIndex:indexPath.row];
    //重新刷新所有的代理方法(方式一)
    //[_tableView reloadData];
    //從tableview中刪除對應的行(方式二)
    [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];//從左方消失(動畫)
    }else{//插入
    NSString * str = @"sdf";
    //找到對應的組 再插入指定行
    [_dataArray [indexPath.section]insertObject:str atIndex:indexPath.row];
    [_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];
    }
    }

//讓cell可以移動

  • (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{

//更改數(shù)據(jù)源中的數(shù)據(jù)順序
//先保存要移動的數(shù)據(jù)
NSString * number = [[_dataArray objectAtIndex:sourceIndexPath.section]objectAtIndex:sourceIndexPath.row];
[[_dataArray objectAtIndex:sourceIndexPath.section]removeObjectAtIndex:sourceIndexPath.row];
[[_dataArray objectAtIndex:destinationIndexPath.section]insertObject:number atIndex:destinationIndexPath.row];
}
//縮進
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
return indexPath.row;
}

//添加多個按鈕

  • (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewRowAction * deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"刪除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
    //實現(xiàn)方法
    //從總數(shù)據(jù)源里刪掉對應的數(shù)據(jù)
    [_dataArray [indexPath.section]removeObjectAtIndex:indexPath.row];
    //從表格里刪除對應的行
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }];

UITableViewRowAction * singAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"標記" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
}];
UITableViewRowAction * topAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置頂" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSString * str = [[_dataArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
[[_dataArray objectAtIndex:indexPath.section]removeObjectAtIndex:indexPath.row];
[[_dataArray objectAtIndex:indexPath.section]insertObject:str atIndex:0];
//刷新
[tableView reloadData];
}];
return @[deleteAction,singAction,topAction];
}

為表格添加搜索索引

  • (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    NSMutableArray * array = [[NSMutableArray alloc]init];
    for (int i = 'A'; i < 'Z'; i++) {
    [array addObject:[NSString stringWithFormat:@"%c",i]];
    }
    return array;
    }

動態(tài)設置單元格高度

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * string = @"string";
CustomTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:string];
if(cell == nil)
{
cell = [[CustomTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
}

計算動態(tài)desc的高度

CGRect rect = [subDataSource[indexPath.row] boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
cell.detailLabel.frame = CGRectMake(0, 80, [UIScreen mainScreen].bounds.size.width, rect.size.height);
NSLog(@"%f",cell.detailLabel.frame.size.height);
lable的坐標原點的坐標以及寬度一定要和自定義單元格上label的初始值完全相同 只有高度是動態(tài)的
CGRect rect = [subDataSource[indexPath.row] boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
cell.detailLabel.frame = CGRectMake(0, 80, [UIScreen mainScreen].bounds.size.width, rect.size.height);
return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//動態(tài)計算cell高度,固定高度+動態(tài)label的高度
NSString *desc = subDataSource[indexPath.row];
//計算動態(tài)desc的高度
CGRect rect = [desc boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
//100是固定高度 rect.size.height是計算得到的動態(tài)高度
return 80 + rect.size.height;
}

使用xib高度自適應時

//允許單元格自適應
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 44.0;

一個section刷新

NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2];
[tableview reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];

一個cell刷新

NSIndexPath *indexPath=[NSIndexPath indexPathForRow:3 inSection:0];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];

//允許單元格自適應
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 44.0;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,963評論 6 542
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,348評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,083評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,706評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,442評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,802評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,795評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,983評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,542評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 41,287評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,486評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,030評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,710評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,116評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,412評論 1 294
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,224評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,462評論 2 378

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

  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件,我們平時使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,082評論 3 38
  • 1.創(chuàng)建一個UITableView對象 ITableView *tableView = [[UITableView...
    桐生一貓閱讀 1,427評論 0 5
  • UITableView 表格視圖一 UITableView1.1是什么?以列表的方式展示數(shù)據(jù)的一種控件,且繼承自...
    037e3257fa3b閱讀 260評論 0 1
  • 插入行。刪除行。折疊區(qū)。設置索引。刷新表格。注冊cell。 //UITableView的ADD // NSStri...
    nothing_c閱讀 210評論 0 0
  • 版權(quán)聲明:未經(jīng)本人允許,禁止轉(zhuǎn)載. 1. TableView初始化 1.UITableView有兩種風格:UITa...
    蕭雪痕閱讀 2,911評論 2 10