天天看點

iOS學習筆記—— UItableView 控件的簡單使用

        UITableView 可以說是iOS開發中最常用的控件,除了遊戲之外,幾乎所有的應用中獨會出現他的身影。

iOS學習筆記—— UItableView 控件的簡單使用

        使用UITableView控件需要遵守兩種協定 UITableViewDelegate和 UITableViewDataSource。

        常用方法如下:

            1.傳回(每個分區)表單元個數(行數)

                 - (NSInteger) tableView: (UItableView *) tableVIew

              numberOfRowsInSection: (NSInteger)section   

            2.傳回表單元資訊

                - (UITableViewCell) tableView: (UITableVIew *) tableView

                          cellForRowAtIndexPath: (NSInteger) indexPath

            3.分區數

            - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

               設定分區名稱

            - (NSString *)tableView:(UITableView *)tableView 

           titleForHeaderInSection:(NSInteger)section;

            4.選中某行後的響應函數

                 - (void) tableVIew: (UITableView *)tableView

   didSelectRowAtIndexPath: (NSIndexPath *) indexPath

            5. 編輯表單元(添加、删除)

                - (void) tableView: (UITableView) tableView

            commotEditingStyle: (UITableViewCellEditingStytle)editingStytle

             forRowAtIndexPath: (NSIndexPath *) indexPath

              .p.s 指定删除按鈕标題

             - (NSString *) tableView: (UItableView *)tableView

titleForDeleteConfirmationButtonForRowAtIndexPath: (NSIndexPath *)indexPath

            6.表單元移動

            - (void)tableView: (UITableView *) tableView

   moveRowAtIndexPath: (NSIndexPath *) fromIndexPath

                   toIndexPath: (NSIndexPath *) toIndexPath

            .p.s 允許移動

            - (BOOL)tableView: (UITableView *) tableView

 canMoveRowAtIndexPath: (NSIndexPath *) indexPath 

           .p.s 允許編輯

            -(BOOL)tableView: (UITableView *) tableView

  canEditRowAtIndexPath:(NSIndexPath *)indexPath

           7.右側添加一個索引表

            - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

           8.擷取某一行的資訊

            - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;

           9.設定表單元高度(行高)

            - (CGFloat) tableView: (UItableView *) tableView

     heightForRowAtIndexPath: (NSIndexPath *) indexPath

           10.設定單元格顔色

             - (void) tableView: (UITableView *) tableVIew

                  willDisplayCell: (UITableViewCell *) cell

           forRowAtIndexPath: (NSIndexPath *) indexPath

           11. 設定表單元文本縮進

            - (NSInteger) tableVIew: (UITableView *) tableView

indentationLevelForRowAtIndexPath: (NSIndexPath *) indexPath

          12. 添加頁眉、頁腳

            - (NSString *) tableView: (UITableView) tableView

            titleForHeaderInSection: (NSInteger) section

            - (NSString *) tableView: (UITableView) tableView

             titleForFooterInSection: (NSInteger) section

        下為常用代碼片段:

        1.設定表單元資訊

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellWithIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellWithIdentifier];
    }
    NSUInteger row = [indexPath row];
    cell.textLabel.text = [dataList objectAtIndex:row];
    return cell;
}
           

        2. 進入編輯狀态(删除,移動)

- (IBAction)Del:(id)sender {
    [_tableView setEditing:!_tableView.editing animated:YES];
    
    if (_tableView.editing)
        [_DeleteButtonTitle setTitle:@"Done" forState:UIControlStateNormal];
    else
        [_DeleteButtonTitle setTitle:@"Delete" forState:UIControlStateNormal];
}
           

        3.左滑删除

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{                       // 左滑删除
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [dataList removeObjectAtIndex: indexPath.row];
        [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject: indexPath] withRowAnimation: UITableViewRowAnimationAutomatic];
    }
}
           

        4.選中并标記某行

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{                       // 選中響應函數(标記)
    UITableViewCell *cellView = [tableView cellForRowAtIndexPath:indexPath];
    if (cellView.accessoryType == UITableViewCellAccessoryNone) 
        cellView.accessoryType = UITableViewCellAccessoryCheckmark;
    else
        cellView.accessoryType = UITableViewCellAccessoryNone;
}
           

          5.移動代碼

- (BOOL)tableView:(UITableView *)tableView
canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
- (void)tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
      toIndexPath:(NSIndexPath *)toIndexPath {
    NSUInteger fromRow = [fromIndexPath row];
    NSUInteger toRow = [toIndexPath row];
    
    id object = [dataList objectAtIndex:fromRow];
    [dataList removeObjectAtIndex:fromRow];
    [dataList insertObject:object atIndex:toRow];
}