UITableView

UITableView

UITableView繼承自UIScrollView,所以可以滾動。表視圖的每一條數據都是顯示在UITableViewCell對象中。表視圖可以分區顯示數據,每個分區稱為一個section,每一行稱為row,編號都是從0開始。
初始化:

tableView.backgroundColor = [UIColor whiteColor];//設置背景色
//指定代理
tableView.dataSource = self;
tableView.delegate = self;
//設置分割線樣式
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
//設置分隔線顏色
tableView.separatorColor = [UIColor redColor];
[self.view addSubview:tableView];```

##UITableViewDataSource代理方法
tableView: numberOfRowsInSection:設置每個分區的cell的個數(必須實現)
```- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;//每個分區下有10個cell
}```

tableView:cellForRowAtIndexPath:定義cell(必須實現)
```- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //初始化一個tableViewCell
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    //設置tableViewCell的標題
    cell.textLabel.text = @"cell";
    //設置tableViewCell的副標題
    cell.detailTextLabel.text = @"name";
    //設置圖片
    cell.imageView.image = [UIImage imageNamed:@"1.png"];
    return cell;
}```

numberOfSectionsInTableView:設置共有多少個分區
```- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;//共有兩個分區
}```

sectionIndexTitlesForTableView:為表示圖添加索引條
```- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    NSArray *array = @[@"1",@"2"]; 
    return array;
}```

tableView:titleForHeaderInSection:通過代理為每一個分區添加標題
```- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"a";
}```

tableView: canEditRowAtIndexPath:設置可編輯狀態下得表視圖那個單元格課編輯
```- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    //不想首個單元格可編輯。直接返回YES,默認所有單元格都可編輯
    if (indexPath.row == 0)
    {
        return 0;
    }
}```

tableView: canMoveRowAtIndexPath:單元格是否可移動
```- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    //YES默認所有單元格都可以移動
    return YES;
}```

tableView:commitEditingStyle:forRowAtIndexPath:編輯完成之后所執行的代理方法
```- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
      if (editingStyle == UITableViewCellEditingStyleDelete) {
        //刪除單元格
        /**
         *  indexPaths:次參數為數組,數組中的元素為NSIndexPath類型,說明此方法可以刪除多個單元格
         *  Animation:刪除動作的動畫
         */
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
        
    }
    //如果編輯樣式為插入時要做的操作
    else if (editingStyle == UITableViewCellEditingStyleInsert)
    {
        //增加一個單元格
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    }
    else
    {
    }
}```

tableView:moveRowAtIndexPath: toIndexPath:移動完成會執行的代理方法,sourceIndexPath當前你所移動的單元格原來的位置,destinationIndexPath 移動完成后所在的新位置
```- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    //把要移動位置的數據先保存起來
    NSString *string = [NSString stringWithString:[self.dataMutableArray objectAtIndex:sourceIndexPath.row]];
    //刪除原來位置的數據
    [self.dataMutableArray removeObjectAtIndex:sourceIndexPath.row];
    //把此數據插入到新的位置
    [self.dataMutableArray insertObject:string atIndex:destinationIndexPath.row];
}```


##UITableViewDelegate代理方法
tableView:didSelectRowAtIndexPath:點擊cell響應的代理方法
```- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld",indexPath.row);
}```

tableView:heightForRowAtIndexPath:通過代理設置cell的高度
```- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}```

tableView:editingStyleForRowAtIndexPath:設置編輯樣式
```- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //讓最后一行為插入數據樣式
    if (indexPath.row == self.dataMutableArray.count - 1) {
        return UITableViewCellEditingStyleInsert;
    }
    //默認所有單元格都是刪除樣式
    return UITableViewCellEditingStyleDelete;
}```





















最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 概述在iOS開發中UITableView可以說是使用最廣泛的控件,我們平時使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,089評論 3 38
  • 版權聲明:未經本人允許,禁止轉載. 1. TableView初始化 1.UITableView有兩種風格:UITa...
    蕭雪痕閱讀 2,919評論 2 10
  • UITableView 表格視圖一 UITableView1.1是什么?以列表的方式展示數據的一種控件,且繼承自...
    037e3257fa3b閱讀 260評論 0 1
  • 自定義單元格 表格無論有多少中自定義單元格樣式 每一種自定義單元格都有復用的能力所以每一個單元格都要帶有一個靜態局...
    DVWang閱讀 277評論 0 0
  • 1.創建一個UITableView對象 ITableView *tableView = [[UITableView...
    桐生一貓閱讀 1,436評論 0 5