UITableView

自定義單元格

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

自定義單元格純手寫的

需要在自定義單元格類中重寫-(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的時候使用
可以不設(shè)置xib上的indentifier屬性值
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//單元格的標(biāo)識上下必須完全相同
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)建 最終導(dǎo)致內(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;
}

擴(kuò)展

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ù)源部分(先刪除某一行)
    //找到對應(yīng)的組 再刪除指定行
    [_dataArray [indexPath.section]removeObjectAtIndex:indexPath.row];
    //重新刷新所有的代理方法(方式一)
    //[_tableView reloadData];
    //從tableview中刪除對應(yīng)的行(方式二)
    [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];//從左方消失(動畫)
    }else{//插入
    NSString * str = @"sdf";
    //找到對應(yīng)的組 再插入指定行
    [_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];
}
//縮進(jìn)
-(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ù)源里刪掉對應(yīng)的數(shù)據(jù)
    [_dataArray [indexPath.section]removeObjectAtIndex:indexPath.row];
    //從表格里刪除對應(yīng)的行
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }];

UITableViewRowAction * singAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"標(biāo)記" 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)設(shè)置單元格高度

-(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的坐標(biāo)原點的坐標(biāo)以及寬度一定要和自定義單元格上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高度自適應(yīng)時

//允許單元格自適應(yīng)
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];

//允許單元格自適應(yīng)
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ā)布平臺,僅提供信息存儲服務(wù)。

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

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