標籤:
表格控制項
一. 設定資料來源<UITableViewDataSource>
1. 設定資料來源
- (void)viewDidLoad { [super viewDidLoad]; // 設定資料來源 self.tableView.dataSource = self; // 設定代理 self.tableView.delegate = self; // 表格頭部和尾部顯示 self.tableView.tableHeaderView = [[UISwitch alloc] init]; self.tableView.tableFooterView = [[UISwitch alloc] init];}
2. 設定有多少組資料
/** * 有多少組資料 */- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return self.carsGroup.count; }
3. 設定每組有多少行資料
/** * 每一組有多少行 */- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ LDCarGroup *carGroup = self.carsGroup[section]; return carGroup.cars.count;}
4. 設定每一行的資料
/** * 每一行的內容 */- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ LDCarGroup *carGroup = self.carsGroup[indexPath.section]; // 緩衝池的ID標識 static NSString *ID = @"car"; // 從緩衝池中取出可迴圈利用的cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 緩衝池中沒有可迴圈利用的cell if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil]; } LDCar *car = carGroup.cars[indexPath.row]; // 1.映像顯示 cell.imageView.image = [UIImage imageNamed:car.icon]; // 2.標題文字顯示 cell.textLabel.text = car.name; // 3.子標題顯示 cell.detailTextLabel.text = @"Good!!!"; // 4.設定cell右邊的指標 /* UITableViewCellAccessoryCheckmark : 顯示√ UITableViewCellAccessoryDetailButton : 顯示詳情按鈕 UITableViewCellAccessoryDetailDisclosureButton : 顯示詳情按鈕+角括弧 UITableViewCellAccessoryDisclosureIndicator : 顯示 角括弧 UITableViewCellAccessoryNone : 預設右邊指標什麼也不顯示 */ // 4.1 用系統的指標 cell.accessoryType = UITableViewCellAccessoryNone; // 4.2定義View顯示到cell右邊 cell.accessoryView = [[UISwitch alloc] init]; // 5.設定cell的背景色 backgroundView的優先順序 > backgroundColor UIImageView *imageView = [[UIImageView alloc] init]; imageView.image = [UIImage imageNamed:@"btn_left"]; cell.backgroundView = imageView; // 6.設定選中cell時的背景色 UIImageView *imageView1 = [[UIImageView alloc] init]; imageView1.image = [UIImage imageNamed:@"img_01"]; cell.selectedBackgroundView = imageView1; return cell;}
5. 組頭部描述
/** * 頭部描述 */- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ LDCarGroup *carGroup = self.carsGroup[section]; return carGroup.title;}
6. 組尾部描述
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{ return ; }
7. 表格右邊導航條顯示
/** * 返回右邊導航條顯示字串 */- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ return [self.carsGroup valueForKeyPath:@"title"];}二. 代理監聽UITableView<UITableViewDelegate>
1. 監聽選中了哪個cell
/** * 監聽選中了那個cell */int group = 0;int row = 0;- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ LDCarGroup *carGroup = self.carsGroup[indexPath.section]; LDCar *car = carGroup.cars[indexPath.row]; // 設定彈窗 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"車名展示" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確認", nil]; // 設定彈窗類型 /* UIAlertViewStyleDefault : 預設彈窗 UIAlertViewStyleLoginAndPasswordInput : 顯示登入對話方塊 UIAlertViewStylePlainTextInput : 顯示一個明文輸入框 UIAlertViewStyleSecureTextInput : 顯示一個密文輸入框 */ alert.alertViewStyle = UIAlertViewStylePlainTextInput; // 取得文字框,顯示車名 [alert textFieldAtIndex:0].text = car.name; // 顯示彈窗 [alert show]; // 擷取組號和行號 group = (int)indexPath.section; row = (int)indexPath.row;}
2. 監聽取消選中的cell
/** * 監聽取消選中cell */- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section{ }三. 代理監聽UIAlertView彈窗<UIAlertViewDelegate>
1. 監聽點擊了UIAlertView彈窗上的哪個按鈕
** * 監聽點擊了UIAlertView彈窗上哪個按鈕 */- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ // 1.如果點擊左邊的取消按鈕直接放回 if (buttonIndex == 0) return; // 2.擷取文字框最後的文字 NSString *name = [alertView textFieldAtIndex:0].text; // 3.修改模型資料 LDCarGroup *carGroup = self.carsGroup[group]; LDCar *car = carGroup.cars[row]; car.name = name; // 4.重新整理資料 // 4.1 全域重新整理資料 [self.tableView reloadData]; // 4.2 局部重新整理資料 /* UITableViewRowAnimationFade : 閃一下 UITableViewRowAnimationRight : cell從左邊向右滑出重新整理 UITableViewRowAnimationLeft : cell從右邊向左滑出重新整理 UITableViewRowAnimationTop : cell從下向上滑出重新整理 UITableViewRowAnimationBottom : UITableViewRowAnimationNone : 沒有什麼效果 UITableViewRowAnimationMiddle : cell從上向下滑出重新整理 UITableViewRowAnimationAutomatic : 動畫閃現 */ NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:group]; [self.tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationAutomatic];}
iOS開發UI之UITableView的基本使用