0

私のアプリでは、 UITableViewを保持するUIViewControllerが必要です。このUITableViewでは、カスタマイズされたUITableViewCellが必要です(つまり、下の画像に示すように、このCELLで独自の要素(画像、ラベル、ボタン)を定義したいと思います)。そして...ストーリーボードで作成したいと思います。

  • これで、ストーリーボードに要素を設定するのは簡単です。
  • UITableViewを接続してUIViewControllerに設定する方法を理解しています(.hファイルのデリゲートを含み、基本的なテーブルデリゲートメソッドを使用します)。
  • 私がはっきりしていないのは、カスタマイズされたUITableViewCellとそのアウトレットを接続して制御する方法です。UIViewController .hおよび.mファイル内にアウトレットとアクションを作成できますか?個別のUITableViewCell.h/.mファイルを作成し、cellForRowAtIndexPathメソッドで呼び出す必要がありますか?

誰かが私のニーズに最適なアプローチを提案できますか?

ここに画像の説明を入力してください

更新:これは、分離されたMyCell.h/mファイルオプションを使用しているときにcellForRowAtIndexPathで使用したコードです。このコードは、UITableViewが実装されているViewController.mファイルに記述されています。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    static NSString *CellIdentifier = @"ContentCell";

    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

//MyCell is the Objective-C Class I created to manage the table cells attributes.

//@"ContentCell" is what I had entered in the Storyboard>>UITableViewCell as the Cell Identifier.

    if (cell == nil) {
        cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//Here is the place I'm not clear about - Am I supposed to init the cell in a different way? If so, how?    
    }

    cell.contentNameLabel.text = [self.dataArray objectAtIndex: [indexPath row]];

// this is the test I had done to check if the new "MyCell" object is actually getting what I would expect it to get. well... the text gets updated in the relevant label, so i guess it gets it 

    return cell;

}

デバッガーのブレークポイントを使用してアプリを実行すると、コードは常に「if(cell == nil)」をスキップし、新しいMyCellオブジェクトが割り当てられて開始されると想定されるコードを入力しないことがわかります。私が間違っている可能性があるアイデアはありますか?

4

1 に答える 1

2

正解です。カスタムUITableViewCellクラスに一致する個別のUITableViewCell.h/.mファイルを作成し、cellForRowAtIndexPathメソッドでそれらを呼び出します。

ストーリーボードで、カスタムUITableViewCellのクラスをカスタムクラス(CustomTableCellなど)に設定します。

カスタムUITableViewCellクラスには、ストーリーボードに接続するIBOutletsが含まれます。例を次に示します。

CustomTableCell.h:

#import "CustomStuff.h" // A custom data class, for this example

@interface CustomTableCell : UITableViewCell

@property (nonatomic, weak) IBOutlet UILabel *titleLabel;

- (void)configureForCustomStuff:(CustomStuff *)stuff;

@end

CustomTableCell.m:

#import "CustomTableCell.h"

@implementation CustomTableCell

@synthesize titleLabel;

#pragma mark - Configure the table view cell

- (void)configureForCustomStuff:(CustomStuff *)stuff
{
    // Set your outlets here, e.g.
    self.titleLabel.text = stuff.title;
}

@end

次に、cellForRowAtIndexPathメソッドで、セルを構成します。

CustomTableCell *cell = (CustomTableCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomCellID"];

// Your custom data here
CustomStuff *customStuff = <YOUR CUSTOM STUFF>

[cell configureForCustomStuff:customStuff];
于 2012-08-02T12:25:58.737 に答える