UITableView詳細注釋,uitableview注釋
style //普通 UITableViewStylePlain, //分組 UITableViewStyleGrouped//表格視圖 UITableView * tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; //設定資料來源 tableView.dataSource = self; //設定代理 tableView.delegate = self; //分區頭的高度 tableView.sectionHeaderHeight = 30; //分區尾的高度 tableView.sectionFooterHeight = 30; //行高,預設行高是44。// tableView.rowHeight = 100;// tableView.backgroundColor = [UIColor grayColor];/* UITableViewCellSeparatorStyleNone 沒有線 UITableViewCellSeparatorStyleSingleLine 單行線 UITableViewCellSeparatorStyleSingleLineEtched 被石化的單行線 //線的風格 _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;// _tableView.separatorColor = [UIColor redColor]; //線的內邊距 _tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);//行高固定時使用 _tableView.rowHeight = 60; //設定背景View// UIImageView * imageView = [[UIImageView alloc] initWithFrame:tableView.bounds];// imageView.image = [UIImage imageNamed:@"baby.jpg"];// tableView.backgroundView = imageView;//索引地區的背景 _tableView.sectionIndexBackgroundColor = [UIColor whiteColor]; //設定索引文字的顏色 _tableView.sectionIndexColor = [UIColor blueColor]; //cell行數小於多少是展示索引 _tableView.sectionIndexMinimumDisplayRowCount = 100; //選擇索引時的背景顏色 _tableView.sectionIndexTrackingBackgroundColor = [UIColor clearColor];//設定編輯模式 [self.tableView setEditing:YES animated:YES]; //得到所有選中的行數 NSArray * deleteList = [self.tableView indexPathsForSelectedRows];代理 UITableViewDataSource,UITableViewDelegate//幾個section- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.peopleList.count;}//Section是分組,Rows多少行,預設情況下只有一個Section.分組的index為0- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSArray * p = self.peopleList[section]; return p.count;}//cell儲存格,IndexPath索引//UITableViewCell是組成UITableView的儲存格- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell * cell = [[UITableViewCell alloc] init]; cell.textLabel.text = self.peopleList[indexPath.section][indexPath.row]; //clearColor是透明顏色 cell.backgroundColor = [UIColor clearColor]; return cell;}//返回分區頂部標題- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return self.headList[section];}//返回分區尾部標題- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { return self.footList[section];}//選中某一行- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //取消選中某一行 [tableView deselectRowAtIndexPath:indexPath animated:YES];//轉到下一頁 NSString * vcName = self.viewControllers[indexPath.section][indexPath.row]; UIViewController * vc = [[NSClassFromString(vcName) alloc] init]; [self.navigationController pushViewController:vc animated:YES]; }//section空隙之間的顏色- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView * v = [[UIView alloc] init]; v.backgroundColor = [UIColor greenColor]; return v;}//當滾動表格時,這個代理方法,一直調用-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //UITableViewCell的複用機制 static NSString * identifier = @"cellID"; //從複用池裡找對應的cell UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (!cell) { /* UITableViewCellStyleDefault 顯示圖片,顯示輔助圖片,顯示一行文字 UITableViewCellStyleValue1 顯示圖片,顯示輔助圖片,普通文字,標題文字,共佔一排 UITableViewCellStyleValue2 不顯示圖片,顯示輔助圖片,普通文字,標題文字,共佔一排 UITableViewCellStyleSubtitle 顯示圖片,顯示輔助圖片,普通文字,標題文字,共佔兩排 */ cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellID"]; } Industry * industry = self.dataList[indexPath.row]; cell.textLabel.text = industry.name; cell.detailTextLabel.text = industry.state; cell.imageView.image = [UIImage imageNamed:industry.icon]; cell.accessoryType = UITableViewCellAccessoryDetailButton; return cell;}//設定行高方法,如果實現這個代理方法,rowHeight無效,這個主要用於設定可變cell高度//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {// // return 100;//}//顯示索引的題目- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return [self.dataList valueForKey:@"title"];}//返回表格視圖是否可以編輯- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES;}//返回表格視圖是否可以滾動- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { return YES;}//確定編輯的選項/* UITableViewCellEditingStyle UITableViewCellEditingStyleNone 不編輯 UITableViewCellEditingStyleDelete 刪除 UITableViewCellEditingStyleInsert 插入 */- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { //如果是刪除操作 if (editingStyle == UITableViewCellEditingStyleDelete) { //從資料來源刪除資料 [self.dataList removeObjectAtIndex:indexPath.row]; //根據indexPath數組刪除元素 NSArray * deleteIndexs = @[indexPath]; //UITableViewRowAnimation 操作動畫 [tableView deleteRowsAtIndexPaths:deleteIndexs withRowAnimation:UITableViewRowAnimationFade]; }}-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { [self.dataList removeObjectAtIndex:indexPath.row]; //重新整理表格// [tableView reloadData]; //重新整理刪除操作 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { //插入操作 [self.dataList insertObject:@"baby" atIndex:indexPath.row]; [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; }}//移動必須實現的方法//sourceIndexPath 起始位置//destinationIndexPath 目標位置- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { NSString * name = self.dataList[sourceIndexPath.row]; [self.dataList removeObjectAtIndex:sourceIndexPath.row]; [self.dataList insertObject:name atIndex:destinationIndexPath.row]; }//修改刪除按鈕- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath { return @"真的要刪除嗎";}//預設返回UITableViewCellEditingStyleDelete- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { // return UITableViewCellEditingStyleInsert; return UITableViewCellEditingStyleDelete;}- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { //多選 return UITableViewCellEditingStyleInsert|UITableViewCellEditingStyleDelete;}