2

UITableViewCell を拡張するカスタム クラスがあります。2 つのラベルと UISegmentedControl があります。

これが、構成した cellForRowAtIndexPath() です。デバッガーで「セル」を調べると、提供しているすべてのデータが含まれています。しかし、どういうわけか、そのデータは決して適用されません。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MyCell";
    CustomGameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

    MyData *my_data = [rows objectAtIndex:indexPath.row];

    UILabel *my_date = [[UILabel alloc] init];
    my_date.text = my_data.myDate;
    [cell setMyDateLabel:my_date];

    UILabel *my_question = [[UILabel alloc] init];
    my_question.text = my.question;
    [cell setMyQuestionLabel:my_question];


    UISegmentedControl *my_choices = [[UISegmentedControl alloc]
                                        initWithItems:[NSArray arrayWithObjects:my.firstChoice, my.secondChoice, nil]];
    [my_choices setSelectedSegmentIndex:my.choice];
    [cell setMyChoiceSegments:my_choices];

    return cell
}

表示したいデータは現在、viewDidLoad() で作成した配列にあり、「rows」変数を介して cellForRowAtIndexPath() にアクセスできます。

シミュレーターでコードを実行すると、viewDidLoad() で作成した配列の 3 つの要素を表すテーブルに 3 つの行が表示されます。ただし、これらの行の内容は、ストーリーボードで定義したものとまったく同じように見えます。

私は何が欠けていますか?

4

2 に答える 2

2
  1. セルのレイアウトをどこで定義していますか? NIBで?あなたの絵コンテで?プログラムであなたinitWithStyleCustomGameCell? 実装の詳細は、使用するアプローチによって少し異なりますが、ストーリーボードで NIB またはプロトタイプ セルを定義するか、プログラムでコントロールを作成し、フレームを設定addSubviewし、セルに含まれるように実行する必要があります。

  2. あなたのコードはUILabel、デキューされたセルを使用しているかどうかに関係なく、サブビューとして追加するのではなく、新しいオブジェクトを追加しています。したがって、ここには多くの問題があります。カスタム セルの適切な使用例については、Table View Programming GuideのCustomizing Cellsを参照してください。ただし、前述したように、サブクラス化されたレイアウトをどのように設計しているかによって詳細が少し異なるため、ユーザー インターフェイスをどのように設計しているかを指定するまで、コードを提案することを躊躇します。UITableViewCell

于 2012-11-23T07:45:35.657 に答える
2

セルのセル コンテンツ ビューにラベルとセグメント コントロールを追加している必要があります。そうでない場合は、追加してください。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MyCell";
    CustomGameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

    MyData *my_data = [rows objectAtIndex:indexPath.row];

    cell.myDateLabel.text = my_data.myDate;

    cell.myQuestionLabel.text = my.question;

    [cell.myChoiceSegments setSelectedSegmentIndex:my.choice];

    [cell autorelease];
    return cell
}

autoreleaseメモリ管理にも使用します。

于 2012-11-23T07:19:02.693 に答える