私のアプリでは、 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オブジェクトが割り当てられて開始されると想定されるコードを入力しないことがわかります。私が間違っている可能性があるアイデアはありますか?