ビューコントローラ内にカスタムセルを含むテーブルビューがあります。私のテーブルビューは正しく機能します。私がやろうとしているのは、プログラムで下の画像を作成することです。ここで、「ラベル」はカスタムであり、入力に応じて変化するテキストです。これらの2つのラベルを(cellForRowAtIndexPath :)に含めて、セル内での位置を決定するにはどうすればよいですか。画像には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;
}
前もって感謝します
最初にデフォルトの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"を忘れないでください。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加