精通IOS-在表单元中添加子视图

#import <UIKit/UIKit.h>

@interface NameAndColorCellTableViewCell : UITableViewCell

@property(copy,nonatomic) NSString *name;
@property(copy,nonatomic) NSString *color;

@end

NameAndColorCellTableViewCell.h

//
//  NameAndColorCellTableViewCell.m
//  Tabel Cells
//
//  Created by  Jierism on 16/7/21.
//  Copyright © 2016年  Jierism. All rights reserved.
//

#import "NameAndColorCellTableViewCell.h"

@interface NameAndColorCellTableViewCell ()

// 定义两个属性变量
@property(strong,nonatomic) UILabel *nameLabel;
@property(strong,nonatomic) UILabel *colorLabel;

@end

@implementation NameAndColorCellTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // 初始化代码
        // 往表单元里面添加子视图
        // 这里在每个表单元里添加了四个Label
        CGRect nameLabelRect = CGRectMake(0, 5, 70, 15);
        UILabel *nameMaker = [[UILabel alloc] initWithFrame:nameLabelRect];
        nameMaker.textAlignment = NSTextAlignmentRight;  // 右对齐
        nameMaker.text = @"Name:";
        nameMaker.font = [UIFont boldSystemFontOfSize:12];
        [self.contentView addSubview:nameMaker];

        CGRect colorLabelRect = CGRectMake(0, 26, 70, 15);
        UILabel *colorMaker = [[UILabel alloc] initWithFrame:colorLabelRect];
        colorMaker.textAlignment = NSTextAlignmentRight;
        colorMaker.text = @"Color:";
        colorMaker.font = [UIFont boldSystemFontOfSize:12];
        [self.contentView addSubview:colorMaker];

        CGRect nameValueRect = CGRectMake(80, 5, 200, 15);
        self.nameLabel = [[UILabel alloc] initWithFrame:nameValueRect];
        [self.contentView addSubview:_nameLabel];

        CGRect colorValueRect = CGRectMake(80, 25, 200, 15);
        self.colorLabel = [[UILabel alloc] initWithFrame:colorValueRect];
        [self.contentView addSubview:_colorLabel];

    }
    return self;
}

// 重写了Name和Color的set方法,当传递一个新的值时,更新标签的额内容
- (void) setName:(NSString *)n {
    if (![n isEqualToString:_name]) {
        _name = [n copy];
        self.nameLabel.text = _name;
    }
}

- (void)setColor:(NSString *)c {
    if (![c isEqualToString:_color]) {
        _color = [c copy];
        self.colorLabel.text = _color;
    }
}

@end

NameAndColorCellTableViewCell.m

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource>

@end

ViewController.h

//
//  ViewController.m
//  Tabel Cells
//
//  Created by  Jierism on 16/7/20.
//  Copyright © 2016年  Jierism. All rights reserved.
//

#import "ViewController.h"
#import "NameAndColorCellTableViewCell.h"

static NSString *CellTableIdentifier = @"CellTableIdentifier";

@interface ViewController ()

// 定义一个数组和输出接口
@property (copy,nonatomic) NSArray *computers;
@property (weak,nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    // 往数组里定义字典
    self.computers = @[@{@"Name" : @"MacBook Air",@"Color" : @"Sliver"},
                       @{@"Name" : @"MacBook Pro",@"Color" : @"Sliver"},
                       @{@"Name" : @"iMac",@"Color" : @"Sliver"},
                       @{@"Name" : @"Mac Mini",@"Color" : @"Sliver"},
                       @{@"Name" : @"Mac Pro",@"Color" : @"Black"},];
    [self.tableView registerClass:[NameAndColorCellTableViewCell class] forCellReuseIdentifier:CellTableIdentifier];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// DataSource方法

// 返回数组元素个数的行数,这里return的数不能大于元素的个数,否则崩溃
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.computers count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NameAndColorCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier forIndexPath:indexPath];

    NSDictionary *rowData = self.computers[indexPath.row];

    cell.name = rowData[@"Name"];
    cell.color = rowData[@"Color"];

    return cell;
}

@end

ViewController.m

以上代码手动实现了在表单元中添加了4个Label,并显示相关内容,运行效果如图

这个效果还有另外一种实现方法,就是使用storyboard,用nib实现。不过之前与一位师兄交流中得知,以后工作中用代码实现视图布局比较多,因为会解决很多问题。在这之前自己做的都是使用storyboard,虽然现在觉得使用起来会省事,但是到了开发大型的APP的时候who know,right?所以,自己还需要提升用代码实现布局的能力。

时间: 2024-08-05 05:11:06

精通IOS-在表单元中添加子视图的相关文章

关于cell中添加子视图 复用重叠问题的解决方法

问题本质: 因为你要添加的子视图并不是在自定义的cell中实现的,而是根据系统给的UITableViewCell这个类创建的实例,每次进图 cellForRow方法都会创建一个cell,每次都要创建一个子视图添(button,label之类的)加进去,会给占用很大的内存,所以采用了复 用的方法,但是问题就来了,当cell超出界面,从队列中拿过来复用的时候,其中子视图的内容并没有消除,这样你会原来的基础上再创建一个子视图添加上去 遮住了原来的视图,一般视图都是透明的这样的话就变成了一层层的叠加.

iOS开发项目篇—02添加子控制器以及项目分层

iOS开发项目篇—02添加子控制器以及项目分层 一.添加子控制器 1.设置根控制器(自定义) 说明:分析新浪微博应用,观察其整体建构层次.而系统的控制器不能满足项目开发的需求,这里把项目中原有的控制器删除. 自己定义一个TabBarViewController类.让这个类作为window窗口的根控制器. YYAppDelegate.m文件代码: 1 #import "YYAppDelegate.h" 2 #import "YYTabBarViewController.h&qu

iOS开发-自定义重用机制给ScrollerView添加子视图

其实这个问题我很早就想过,只是没有通过去敲代码实现,昨天有人提起,我就巧了一下 不知道大家打印郭tableview:cellforrow中cell初始的次数,也就是重用池中的cell个数,这个是固定的,比如屏幕最多可以显示4个cell那么重用池个数可能为5个6个或者7左右,我就想了如果scrollview去实现,子视图也应该是有一个个数的,那么如何去实现重用池呢?我想到了数组,可变数组,我在loadview中将scrollview的可滑动区域设置为10000,然后给scrollview每隔20个

iOS自定义alertView,继承自UIView,可以添加子视图,标题图片+文字

自定义alertView,继承自UIView,可以在消息区域添加子视图:addCustomerSubview 标题可以有图片+文字构成, 只支持两个按钮操作 // - 在需要alert的控制器调用 alertView show 方法 CustomAlertView *alertView = [[CustomAlertView alloc] initWithTitle:@"提示" message:@"dylan_lwb_" delegate:self cancelBu

[iOS]在NavigationController中的ScrollView中的子视图都会下移64个像素

情况是这样的: 我有一个UINavigationController,设置为self.window的root视图, 然后有一个UIVIewController是UINavigtionController的根视图. 然后在UIViewController中加入一个ScrollView 在ScrollView中加入一个view. 此时发现. scrollView并没有自动下移64像素,而 ScrollView中的子视图VIew自动下移了64个像素. 上图: // 解决方法 self.automati

在UIViewController的view上添加子视图不显示

第一种方式:通过代码方式创建视图控制器UIViewController,添加子视图,需要留意创建视图的size,如果是为0是看不到的 第二种方式:xib或storyboard方式创建视图控制器UIViewController,需要留意xib是否与视图变量之间建立的关系,没有建立关系子视图是添加到视图view中,但是就是不能系显示. 如图:

新浪微博开发-添加子视图控制器&amp;设置颜色

一.添加子视图控制器 二.设置颜色 设置颜色:两种方法 一种较为繁琐,详见视频 第二种: //设置颜色 self.tabBar.tintColor = UIColor.orangeColor()

精通IOS开发-表视图的使用

// // ViewController.m // Simple Table // // Created by Jierism on 16/7/20. // Copyright © 2016年 Jierism. All rights reserved. // #import "ViewController.h" @interface ViewController ()<UITableViewDelegate,UITableViewDataSource> // 声明一个数组,

在工作表左侧中添加TreeView控件

开发环境基于VSTO:visual studio 2010,VB .Net,excel 2007,文档级别的定制程序. 需求是在sheet的左侧停靠System.Windows.Forms.TreeView控件,实现类似资源浏览器的效果,另外,tree节点使用自定义的图标,支持复选框. 首先准备好树节点的图标,使用visual studio 2010自带的图标可以省去很多麻烦(在安装目录\Common7\VS2010ImageLibrary).我挑选了4个16x16大小的图标拷贝到vsto工程下