#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(, , , );
UILabel *nameMaker = [[UILabel alloc] initWithFrame:nameLabelRect];
nameMaker.textAlignment = NSTextAlignmentRight; // 右对齐
nameMaker.text = @"Name:";
nameMaker.font = [UIFont boldSystemFontOfSize:];
[self.contentView addSubview:nameMaker]; CGRect colorLabelRect = CGRectMake(, , , );
UILabel *colorMaker = [[UILabel alloc] initWithFrame:colorLabelRect];
colorMaker.textAlignment = NSTextAlignmentRight;
colorMaker.text = @"Color:";
colorMaker.font = [UIFont boldSystemFontOfSize:];
[self.contentView addSubview:colorMaker]; CGRect nameValueRect = CGRectMake(, , , );
self.nameLabel = [[UILabel alloc] initWithFrame:nameValueRect];
[self.contentView addSubview:_nameLabel]; CGRect colorValueRect = CGRectMake(, , , );
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?所以,自己还需要提升用代码实现布局的能力。

iOS开发-在表单元中添加子视图的更多相关文章

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

    问题本质:   因为你要添加的子视图并不是在自定义的cell中实现的,而是根据系统给的UITableViewCell这个类创建的实例,每次进图 cellForRow方法都会创建一个cell,每次都要创 ...

  2. iOS开发(OC)中的命名规范

    开小差:最近发现自己有一个经验主义的毛病,不太容易接受新的知识,这对从事技术研发的人来说不太合理,需要改之. 正文:通过读写大量代码我有自己的一套编程思路和习惯,自认为自己的编码习惯还是不错的,代码结 ...

  3. IOS开发之表视图(UITableView)

    IOS开发之表视图(UITableView)的基本介绍(一) (一):UITableView的基本概念 1.在IOS开发中,表视图的应用十分广泛和普及.因此掌握表视图的用法显得非常重要.一般情况下对于 ...

  4. iOS开发拓展篇—xib中关于拖拽手势的潜在错误

    iOS开发拓展篇—xib中关于拖拽手势的潜在错误 一.错误说明 自定义一个用来封装工具条的类 搭建xib,并添加一个拖拽的手势. 主控制器的代码:加载工具条 封装工具条以及手势拖拽的监听事件 此时运行 ...

  5. IOS开发效率之为Xcode添加常用的代码片段

    IOS开发效率之为Xcode添加常用的代码片段 原文地址:http://blog.csdn.net/pingchangtan367/article/details/30041285 tableview ...

  6. iOS开发实用技巧—Objective-C中的各种遍历(迭代)方式

    iOS开发实用技巧—Objective-C中的各种遍历(迭代)方式 说明: 1)该文简短介绍在iOS开发中遍历字典.数组和集合的几种常见方式. 2)该文对应的代码可以在下面的地址获得:https:// ...

  7. 解决UIViewController中添加子控制器viewWillAppear不调用问题

    问题描述: 我在UICollectionViewController中添加子控制器数组, 并在cellForItem中把子控制器数组中对应的控制器对应的view添加到了UICollectionView ...

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

    情况是这种: 我有一个UINavigationController,设置为self.window的root视图, 然后有一个UIVIewController是UINavigtionController ...

  9. UIScrollView中添加一个视图,实现让其始终固定在某个位置

    ScrollView中添加一个视图,实现让其始终固定在某个位置,如最底部的位置.方法是自定义一个继承UIScrollView,重写它的layoutSubviews方法.代码如下: #import &q ...

随机推荐

  1. 安装Hadoop系列 — 安装JDK-8u5

    安装步骤如下: 1)下载 JDK 8 从http://www.oracle.com/technetwork/java/javasebusiness/downloads/ 选择下载JDK的最新版本 JD ...

  2. 研究系统IO和glib IO的关系

    PS:这里的缓冲与非缓冲的区别是相对于用户进程,下文的“非缓冲文件系统”指用户的程序没有缓冲区,不要误解为系统没有缓冲区. 1.缓冲文件系统缓 冲文件系统的特点是:在内存开辟一个“缓冲区”,为程序中的 ...

  3. memcache简易教程

    1.  memcache是什么 memcache是一个高性能的分布式的内存对象缓存系统,用于动态Web应用以减轻数据库负担.它通过在内存中缓存数据和对象,来减少读取数据库的次数.从而提高动态.数据库驱 ...

  4. 手动添加 memcached.jar包

    由于目前java memcached client没有官方的maven repository可供使用,因此使用时需要手动将其安装到本地repository. java memcached client ...

  5. CentOS7.1 JDK安装 和 CentOS7.1配置yum源

    1.卸载自带OPENJDK #查看自身jdk java -verson #查看自身安装的java  rpm -qa | grep java #显示如下 python-javapackages-3.4. ...

  6. delphi使用 第三方控件

    第三方控件安装时必须把所有的pas,dcu,dpk,res等文件复制到你的Lib目录下 然后通过dpk进行安装 安装后会多出来新的控件面板,新控件就在那里了 当然也有一些控件会安装到原有的面板上 比如 ...

  7. Android2.3.7源码结构分析

    对Andorid系统进行分析或者系统功能定制的时候,我们经常需要在众多文件中花费大量时间定位所需关注的部分.为了减轻这部分枯燥而不可避免的工作,本文对2.3.7版本的源码结构进行了简单分析.希望对刚加 ...

  8. Android开发之Service的写法以及与Activity的通信

    Service的总结: 1.按运行地点分类: 类别 区别  优点 缺点   应用 本地服务(Local) 该服务依附在主进程上,  服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外 ...

  9. jquery提示气泡

    <link href="css/manhua_hoverTips.css" type="text/css" rel="stylesheet&qu ...

  10. ajax post提交数据, input type=submit 返回prompt aborted by user

    添加 return false;否则就报prompt aborted by user异常