LoginSignup
7
10

More than 5 years have passed since last update.

【Objective-C】UITableViewを使用する際によく使うデリゲートメソッド(テンプレート)やCustomCellの設定

Last updated at Posted at 2015-11-25

UITableViewのDelegateとDataSourceをセット

Storyboard、xib等でセットしても良し

.m
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

セルのカスタムクラスをUITableViewに登録

viewDidLoadやawakeFromNib等で記載

  • nibを登録する場合
.m
    // セルの設定
    NSString *cellName = NSStringFromClass([CustomCell class]);
    [self.tableView registerNib:[UINib nibWithNibName:cellName bundle:nil] forCellReuseIdentifier:cellName];

    // セクションの設定
    NSString *sectionName = NSStringFromClass([CustomSection class]);
    [self.tableView registerNib:[UINib nibWithNibName:sectionName bundle:nil] forHeaderFooterViewReuseIdentifier:sectionName];

  • クラスを登録する場合
.m
    // セルの設定
    [self.tableView registerClass:[CustomCell class] forCellReuseIdentifier:NSStringFromClass([CustomCell class])];

    // セクションの設定
    [self.tableView registerClass:[CustomSection class] forHeaderFooterViewReuseIdentifier:NSStringFromClass([CustomSection class])];


UITableView Datasource(実装必須

.m
/**
 *  対象TableViewのセクション内行数を設定します
 *
 *  @param tableView 対象TableView
 *  @param section   対象セクション
 *
 *  @return セクション内行数
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 0;
}


/**
 *  セルを設定します
 *
 *  @param tableView 対象TableView
 *  @param indexPath 対象IndexPath
 *
 *  @return 設定したセル
 */
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellName = NSStringFromClass([CustomCell class]);
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName forIndexPath:indexPath];
    return cell;

    /* 標準Cellを使用する場合
    NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    cell.textLabel.text = @"セルテキスト";
    return cell;
     */
}

UITableView Datasource(optional

.m
/**
 *  対象TableViewのセクション数を設定します
 *
 *  @param tableView 対象TableView
 *
 *  @return セクション数
 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


/**
 *  セクションのヘッダータイトルを設定します
 *
 *  @param tableView 対象テーブル
 *  @param section   対象セクション
 *
 *  @return 設定するタイトル
 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return @"セクションヘッダー1";
            break;
        case 1:
            return @"セクションヘッダー2";
            break;
        default:
            return @"";
            break;
    }
}


/**
 *  セクションのフッタータイトルを設定します
 *
 *  @param tableView 対象テーブル
 *  @param section   対象セクション
 *
 *  @return 設定するタイトル
 */
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return @"セクションフッター1";
            break;
        case 1:
            return @"セクションフッター2";
            break;
        default:
            return @"";
            break;
    }
}


UITableView Delegate(optional

.m
/**
 *  セルタップ時に呼ばれます
 *
 *  @param tableView 対象TableView
 *  @param indexPath 対象IndexPath
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}


/**
 *  セルの高さを設定します
 *
 *  @param tableView 対象TableView
 *  @param indexPath 対象IndexPath
 *
 *  @return セルの高さ
 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}


/**
 *  セクションヘッダーの高さを設定します
 *
 *  @param tableView 対象TableView
 *  @param section   対象Section
 *
 *  @return セクションヘッダーの高さ
 */
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 22;
}


/**
 *  セクションフッターの高さを設定します
 *
 *  @param tableView 対象TableView
 *  @param section   対象Section
 *
 *  @return セクションフッターの高さ
 */
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 22;
}

/**
 *  対象セクションを設定します。
 *
 *  @param tableView 対象TableView
 *  @param section   対象セクション
 *
 *  @return 設定したセクション
 */
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    CustomSection *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass([CustomSection class])];    
    return header;
}


7
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
10