目錄
一、分為兩種
二、顯示數據步驟;
1、設置協議
2、實現三個方法
3、設置行高
4、常見屬性
1、 設置點擊選中樣式顏色
2、設置常用自定義Cell;
5、UITableViewCell重用
6、設置右側title
7、實現的多個方法匯總
8、刷新TableView數據
9、滾動TableView到指定位置(可以滾動到底部)
10、自定義側滑刪除和側滑多個按鈕
11、設置編輯模式
一、分為兩種
UITableView
二、顯示數據步驟:
1、設置協議
self.tableView.dataSource=self;//設置協議
interfaceViewController ()<UITableViewDataSource>//
2、實現三個方法
//設置TablerView顯示幾組數據,默認分一組; - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 4; } //設置UITabView每組顯示幾行數據 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 7; } //設置每一行的每一組顯示單元格的什么內容 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //創建一個單元格並返回 UITableViewCell *tableViewSell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; tableViewSell.textLabel.text= @"nihao"; tableViewSell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;//顯示右邊箭頭 tableViewSell.accessoryView=[[UISwitch alloc]init];//自定義右邊顯示控件 return tableViewSell; } - (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return @"標題"; } - (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{ return @"底部描述"; }
3.設置行高
//如果行高一致設置行高 self.tableView.rowHeight=120;
//對於每行的行高不一致的情況,無法通過rowHeight來設置,此時只能通過代理來設置 self.tableView.delegate=self;//1.添加代理 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>//2.實現代理 - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if(indexPath.row%2){ return 60; }else{ return 120; } }
4.常見屬性
UITableViewCell常見屬性: *imageView *textLable *detailTextLable accessoryType//右側常用按鈕 accessoryView//右側自定義按鈕 backgroundColor//設置單元格背景顏色; backgroundView//設置單元格背景控件:可以是控件 //設置選中的單元格的背景顏色 UIView *bgView=[[UIView alloc]init]; bgView.backgroundColor=[UIColor greenColor]; tableViewSell.selectedBackgroundView=bgView;
//設置選中樣式顏色,不顯示選中顏色
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UITableView常見屬性 self.tableView.rowHeight=120;// 設置行高 self.tableView.separatorColor;//分割線的顏色 self.tableView.separatorStyle=UITableViewCellSelectionStyleNone;//分割線的樣式;不顯示分割線 self.tableView.tableHeaderView=[UIButton buttonWithType:UIButtonTypeContactAdd];//一般放廣告 self.tableView.tableFooterView=[UIButton buttonWithType:UIButtonTypeContactAdd];//一般放加載更多
5.UITableViewCell重用:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ Groups *group=self.groups[indexPath.section]; Car *car=group.cars[indexPath.row]; static NSString *ID=@"car_cell"; UITableViewCell *tableCell= [tableView dequeueReusableCellWithIdentifier:ID]; if(tableCell==nil){ tableCell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } tableCell.textLabel.text=car.name; tableCell.imageView.image=[UIImage imageNamed:car.icon]; return tableCell; }
6.設置右側title
//設置右側title - (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{ return [self.groups valueForKeyPath:@"title"]; }
7.實現的多個方法匯總
//設置組數 - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{ return self.groups.count; } //設置組名 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ Groups *groups= self.groups[section]; return groups.title; } //設置每組的條目數 - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ Groups *groups= self.groups[section]; return groups.cars.count; } //設置右側title - (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{ return [self.groups valueForKeyPath:@"title"]; } //返回單元格 - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ Groups *group=self.groups[indexPath.section]; Car *car=group.cars[indexPath.row]; static NSString *ID=@"car_cell"; UITableViewCell *tableCell= [tableView dequeueReusableCellWithIdentifier:ID]; if(tableCell==nil){ tableCell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } tableCell.textLabel.text=car.name; tableCell.imageView.image=[UIImage imageNamed:car.icon]; return tableCell; }
8.刷新TableView數據
//刷新所有 [self.tableView reloadData]; //刷新指定的組 NSIndexSet *nsIndexSet= [NSIndexSet indexSetWithIndex:1]; [self.tableView reloadSections:nsIndexSet withRowAnimation:UITableViewRowAnimationAutomatic]; //刷新指定的行 ,組和行參數 NSIndexPath *idxPath=[NSIndexPath indexPathForRow:1 inSection:1];//幾列幾組 [self.tableView reloadRowsAtIndexPaths:@[idxPath] withRowAnimation:UITableViewRowAnimationAutomatic];
// 刪除當前indexPath
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
9.滾動TableView到指定位置(可以滾動到底部)
//把tableview滾動到最底部 NSIndexPath *idxPath=[NSIndexPath indexPathForRow:1 inSection:0];//幾列幾組 [self.tableView scrollToRowAtIndexPath:idxPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
10.自定義側滑刪除和側滑多個按鈕
//重寫這個方法才能實現左滑刪除功能,出現一個按鈕 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { [self.dataArray removeObjectAtIndex:indexPath.row]; [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop]; //[self.tableView reloadData];//刷新 } //重寫這個方法才能實現多個按鈕點擊,如果實現這個方法,commitEditingStyle將無效 - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"關注" handler:^(UITableViewRowAction *_Nonnull action, NSIndexPath *_Nonnull indexPath) { NSLog(@"關注"); }]; action.backgroundColor = [UIColor grayColor]; UITableViewRowAction *actionDelete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"刪除" handler:^(UITableViewRowAction *_Nonnull action, NSIndexPath *_Nonnull indexPath) { NSLog(@"刪除"); }]; actionDelete.backgroundColor = [UIColor redColor]; UITableViewRowAction *actionDefault = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"默認" handler:^(UITableViewRowAction *_Nonnull action, NSIndexPath *_Nonnull indexPath) { NSLog(@"默認"); }]; actionDefault.backgroundColor = [UIColor greenColor]; return @[action, actionDelete, actionDefault]; } //左滑刪除文字定義 - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath { return @"刪除數據"; }
11、設置編輯模式
//設置編輯模式 self.tableView.editing = YES;
//設置編輯模式動畫
[self.tableView setEditing:YES animated:YES];
注:
//數據賦值
[self setValuesForKeysWithDictionary:dict]; NSMutableArray *arrayM=[NSMutableArray array]; for (NSDictionary *dictCar in dict[@"cars"]) { [arrayM addObject: [[Car alloc] initWithCar:dictCar]]; } self.cars=arrayM;