您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

UITableView 進階整理:所有常用 Method 與 Cell 客製化一次看

編輯:iOS 開發綜合

UITableView 的 API 很多,但真正會用到的其實就那幾組。這篇把它們依功能分類整理,當成查閱用的對照表。

一、建立表格

DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)];
[DataTable setDelegate:self];
[DataTable setDataSource:self];
[self.view addSubview:DataTable];
[DataTable release];

二、常用 Method 分類

資料來源(dataSource)

// 分區數量,預設 1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}

// 每個分區的行數,預設 1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [data count];
}

// 繪製 cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:SimpleTableIdentifier] autorelease];
    }
    cell.imageView.image = image;              // 一般狀態的圖
    cell.imageView.highlightedImage = highlightImage;  // 選中後的圖
    cell.textLabel.text = @"...";
    return cell;
}

外觀與版面

// 行縮進
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [indexPath row];
}

// 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

// 分隔線樣式(設為 None 即隱藏)
[tableView setSeparatorStyle:UITableViewCellSelectionStyleNone];

// 分隔線顏色
tableView.separatorColor = [UIColor blueColor];

// 內容尺寸(手動控制可捲動範圍)
[topicsTable setContentSize:CGSizeMake(0, controller.promiseNum * 44)];

滾動與定位

// 捲到指定位置
[TopicsTable setContentOffset:CGPointMake(0, promiseNum * 44 + Chapter * 20)];

// 捲到某個 row / section
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
                 atScrollPosition:UITableViewScrollPositionBottom
                         animated:NO];

選取與編輯

// 點擊 cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];  // 反白立刻消失
}

// 攔下特定列的選取(這裡禁止選第一行)
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath row] == 0) return nil;
    return indexPath;
}

// 程式化選取
NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:section];
[TopicsTable selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionNone];

// 是否允許左滑刪除
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

// 進入編輯狀態
- (void)tableView:(UITableView *)tableView
    commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
     forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 實作刪除/插入邏輯
}

// 自訂刪除確認按鈕文字
- (NSString *)tableView:(UITableView *)tableView
    titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"刪除";
}

索引與標題

// 右側索引列
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return TitleData;
}

// 每個 section 的標題
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"";
}

三、在 cell 上放多行 UILabel

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
                                       reuseIdentifier:CellIdentifier] autorelease];
        UILabel *Datalabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 320, 44)];
        [Datalabel setTag:100];
        Datalabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [cell.contentView addSubview:Datalabel];
        [Datalabel release];
    }
    UILabel *Datalabel = (UILabel *)[cell.contentView viewWithTag:100];
    [Datalabel setFont:[UIFont boldSystemFontOfSize:18]];
    Datalabel.text = [data.DataArray objectAtIndex:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

自訂子檢視只在建立時加一次,之後用 tag 取回並更新內容——這與前面提到的重用機制是同一個道理:建立成本高的動作放在 cell == nil 分支,每輪都做的只有賦值。

四、三個常搞混的列舉

// 選中 cell 時的樣式
typedef enum {
    UITableViewCellSelectionStyleNone,
    UITableViewCellSelectionStyleBlue,
    UITableViewCellSelectionStyleGray
} UITableViewCellSelectionStyle;

// cell 右側附件的樣式
typedef enum {
    UITableViewCellAccessoryNone,                       // 不顯示
    UITableViewCellAccessoryDisclosureIndicator,         // 灰色箭頭,不接收事件
    UITableViewCellAccessoryDetailDisclosureButton,      // 藍色按鈕+箭頭,可點擊
    UITableViewCellAccessoryCheckmark                   // 勾選符號
} UITableViewCellAccessoryType;

// 分隔線樣式
typedef enum {
    UITableViewCellSeparatorStyleNone,
    UITableViewCellSeparatorStyleSingleLine
} UITableViewCellSeparatorStyle;

實務上最常混淆的是 setSeparatorStyle:(表格層級)與 UITableViewCellSelectionStyle(cell 選取層級),兩者名字很像但作用完全不同。另外,自訂 cell 時若要撐多行文字,記得把 label 的 numberOfLines 設為 0 並搭配 sizeThatFits: 動態算行高,單靠固定 44pt 是不夠的。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved