1

ビューコントローラー内にカスタムセルを含むテーブルビューがあります。私のテーブルビューは正しく動作します。私がやろうとしているのは、以下の画像をプログラムで開発することです。「ラベル」はカスタムのテキストで、一部の入力に応じて変化します。これら 2 つのラベルを (cellForRowAtIndexPath:) に含めて、セル内の位置を決定するにはどうすればよいですか。画像には 2 つの異なるテーブル セルが含まれています。

画像には 2 つの異なるテーブル セルが含まれています

ストーリーボードを使用してそれを行う方法は知っていますが、xCode 4.5 で動的セルを使用しているため、プログラムで行う必要があります。

画像はインデックス セル 1 と 2 を参照しています。これまで、セルごとに 1 つのテキスト ラベルしか含めることができませんでした。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {



static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier];
}




    switch ([indexPath row])
    {
        case 0:
        {
           // image cell - image resize and centred


            UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(30,2, 180, 180)];
            imv.image=[UIImage imageNamed:@"test1.jpg"];


            imv.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
            [cell.contentView addSubview:imv];

            imv.center = CGPointMake(cell.contentView.bounds.size.width/2,cell.contentView.bounds.size.height/2);

            cell.accessoryType = UITableViewCellAccessoryNone;

            break;
        }
        case 1:
        {


            cell.textLabel.text = @"Name";


            break;
        }

        case 2:
        {
            cell.textLabel.text = @"Manufacturer";

            break;
        }

        case 3:
        {
            cell.textLabel.text = @"Overall Score";

            break;
        }

        case 4:
        {
            cell.textLabel.text = @"Description";
            cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

            break;
        }

        case 5:
        {
            cell.textLabel.text = @"Videos";
            cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

            break;
        }
            }


    return cell;

}

前もって感謝します

4

1 に答える 1

1

最初にデフォルトの UITableViewCell スタイルを確認します: UITableViewCellStyleDefault、UITableViewCellStyleValue1、UITableViewCellStyleValue2、UITableViewCellStyleSubtitle。

それらを使用できる場合は、UITableViewCell をサブクラス化する必要はありません。それ以外の場合は、それを実行して、UIView と 2 つの UILabel の 3 つのプロパティを配置する必要があります。これは、デフォルトのセル スタイルを使用しないと、セル要素を移動または追加できないためです。

UITableViewCell サブクラスには次のコードが必要です。

@interface UITableViewCellSubClass : UITableViewCell
@property (nonatomic, strong) UIView *view;
@property (nonatomic, strong) UILabel *label1;
@property (nonatomic, strong) UILabel *label2;
@end

@implementation UITableViewCellSubClass
@synthesize view;
@synthesize label1;
@synthesize label2;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    view = [[UIView alloc] initWithFrame:self.frame];
    [self addSubview:view];
    // initiate label1 with position (10,10,150,20)
    label1 = [[UILabel alloc] initWithFrame:CGRectMake(10,10,150,20)];
    // initiate label2 with position (170,10,150,20)
    label2 = [[UILabel alloc] initWithFrame:CGRectMake(170,10,150,20)];
    [view addSubview:label1];
    [view addSubview:label2];
}
return self;
}
@end

次に、 cellForRowAtIndex: メソッドでそれを返すことができます。基本的には次のとおりです。

UITableViewCellSubclass *cell = [[UITableViewCellSubclass alloc] initWithStyle:UITableViewCellDefault reuseIdentifier:@"cell"];
[cell.label1 setText:@"text"];
[cell.label2 setText:@"more text"];

#import "UITableViewCellSubClass.h" を忘れないでください。

于 2014-07-07T16:01:40.370 に答える