IOS UI基礎07,iosui07
TableView
屬性
// 設定每一行cell的高度 @property (nonatomic)CGFloat rowHeight; // 設定每一組頭部的高度 @property (nonatomic)CGFloat sectionHeaderHeight; // 設定分割線顏色 @property (nonatomic, retain) UIColor *separatorColor // 設定表頭控制項 @property (nonatomic, retain) UIView *tableHeaderView; // 設定表尾控制項 @property (nonatomic, retain) UIView *tableFooterView; // 2.組頭組尾的高 self.tableView.sectionHeaderHeight = 55; self.tableView.sectionFooterHeight = 22; // 3.設定整個tablView的頭部/尾部視圖 self.tableView.tableHeaderView = [[UISwitch alloc] init]; self.tableView.tableFooterView = [UIButton buttonWithType:UIButtonTypeInfoDark]; // 4.設定我們分割線顏色(clearColor相當於取消系統分割線) //self.tableView.separatorColor = [UIColor clearColor]; // 5.設定分割線樣式 self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
- 資料來源方法
//cell有多少組-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView //告訴tableView第section組有多少行-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section//告訴tableView每一行顯示什麼內容(tableView的每一行都是UITableViewCell)-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath //告訴tableView第section組的頭部標題文字-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section // 告訴tableView第section組的尾部標題文字-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section//返回每一組的索引標題- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView // 返回每個cell的高度 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath//刪除資料- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;//
- 代理方法
// 只要實現了這個方法,左滑出現按鈕的功能就有了(一旦左滑出現了N個按鈕,tableView就進入了編輯模式, tableView.editing = YES) -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath //左滑cell時出現什麼按鈕 -(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewRowAction *action0 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"關注" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { NSLog(@"點擊了關注"); // 收回左滑出現的按鈕(退出編輯模式) tableView.editing = NO; }]; UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"刪除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { [self.wineArray removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; }]; return @[action1, action0];} //代理方法擷取點滴選中行,行號 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; //允許編輯選中行 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;