tableviewcell label、buttonを追加
2673 ワード
1.tableviewcellにlabelを追加し、まず
次にlabelのプロパティを定義できます
label.backgroundColor = [UIColor grayColor];//背景色の設定
label.text = @"Hello World";//設定内容
label.textColor = [UIColor blueColor];//テキスト色
label.backgroundColor = [UIColor clearColor];//labelの背景色の透明度を設定する
ここで私が追加した最初の3行の色は違います.
[cell.contentView addSubview:label];[self.view addSubview:label]に変更すると、tableviewではなくtableviewにlabelが作成されます.
2.tableviewcellにbuttonを追加
よく使われるbuttonのプロパティ
button.frame = CGRectMake(150,30,50,20);//buttonの位置
[button settitle:@"クリック"forState:UIcontrolStateNormal];
button.backgroundColor = [UIColor blackColor];
3 cellでimageを作成する
もう一つの方法がある
この方法でこの画像をcellの一番左側のimageviewに表示します
CGRect labelRect = CGRectMake(X, Y,length, width);//x、y
UILabel *label = [[UILabel alloc] initWithFrame:labelRect];
次にlabelのプロパティを定義できます
label.backgroundColor = [UIColor grayColor];//背景色の設定
label.text = @"Hello World";//設定内容
label.textColor = [UIColor blueColor];//テキスト色
label.backgroundColor = [UIColor clearColor];//labelの背景色の透明度を設定する
ここで私が追加した最初の3行の色は違います.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
// label
CGRect labelRect = CGRectMake(120, 10,130, 20);
UILabel *label = [[UILabel alloc] initWithFrame:labelRect];
label.text = @"hello world!!!";
label.textColor = [UIColor redColor];
[cell.contentView addSubview:label];
label.font = [UIFont boldSystemFontOfSize:16];
}
return cell;
}
[cell.contentView addSubview:label];[self.view addSubview:label]に変更すると、tableviewではなくtableviewにlabelが作成されます.
2.tableviewcellにbuttonを追加
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
よく使われるbuttonのプロパティ
button.frame = CGRectMake(150,30,50,20);//buttonの位置
[button settitle:@"クリック"forState:UIcontrolStateNormal];
button.backgroundColor = [UIColor blackColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(150,30,50,20);
[button setTitle:@" " forState:UIControlStateNormal];
button.backgroundColor = [UIColor blackColor];
cell.accessoryView = button;
3 cellでimageを作成する
UIImage *image = [UIImage imageNamed:@"[email protected]"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(50, 10, 45, 45);
[cell.contentView addSubview:imageView];
もう一つの方法がある
cell.imageView.image = [UIImage imageNamed:@"[email protected]"];
この方法でこの画像をcellの一番左側のimageviewに表示します