`
xiaotongeye
  • 浏览: 18023 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

tableviewcell添加label、button

阅读更多
1.给tableviewcell添加label,首先
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的背景色透明
在这我添加的前三行颜色不一样
- (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]则label将建立在tableview中,而不是tableviewcell中
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:@"meitu@2x.png"];       
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        imageView.frame = CGRectMake(50, 10, 45, 45);
        [cell.contentView addSubview:imageView];

还有一种办法
cell.imageView.image = [UIImage imageNamed:@"meitu@2x.png"];

用这个办法该图片显示在cell最左侧imageview中
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics