iOS開發UI之UITableView的基本使用

來源:互聯網
上載者:User

標籤:

表格控制項

一. 設定資料來源<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的基本使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.