2

私はたくさんの例を見てきましたが、最良の答えを見つけることができません。

高さが120で、3つのUILabelとUIImageViewを持つカスタムセルがあるとします。

セルを初期化する

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

すべてのアプローチの中で、どれが最も最適ですか?

4

3 に答える 3

1

UITableViewCellをサブクラス化してカスタムUITableviewCellを作成し、メソッドinitWithStyleをオーバーライドします。このinitメソッドにラベルを割り当て、このメソッドのセルにこのラベルを追加することもできます。

nibファイルを使用してUItableViewCellを作成することもできます。

http://ijoshsmith.com/2011/07/16/creating-a-custom-uitableviewcell-in-ios-4/

于 2012-07-17T17:39:34.537 に答える
1

あなたがしなければならないのはこのようなものです:

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Prototype Cell"];
    UILabel *label;
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                      reuseIdentifier:@"Prototype Cell"];
        label = [[UILabel alloc] init];
        label.tag = LABEL_TAG;  //Suppose LABEL_TAG is defined someplace else
        [cell.contentView addSubview: label];
    }
    else 
    {
        label = [cell.contentView viewWithTag:LABEL_TAG];
    }


    label.text = @"Hello World";

    //Suppose you want the label at x coordinate 50
    label.frame = CGRectMake(0,0,50,120); 

    //Follow similar steps for all of your subviews

    return cell;
}
于 2012-07-17T17:25:05.760 に答える
1

カスタムテーブルを作成するためのおそらく最良の答えは、以下のリンクにあります。そこで表示されるコードについて具体的な質問がある場合は、さらに質問してください。

http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html

于 2012-07-17T17:25:14.917 に答える