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;
}```