#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. Git教程(8)Git几种工作方式

    1,集中共享式(1个仓库) 其中角色: 1个远程仓库,N个开发者. 工作方式: 集中式系统:所有开发者共享同一个远程仓库.每次推送数据到远程仓库时都要先更新一下. 利用 Git 的分支模型,通过同时在 ...

  2. VS2005下开发PPC2003和WM50编译器一些设置

    1.vs2005开发WM5时,编译器和linker的选项配合问题 链接:http://www.mivi.name/blog/index_en.php?itemid=258 首先说ARM4 ARM4T ...

  3. poj1989

    一道非常神奇的题目 var v:array[0..10010] of boolean; n,k,i,x,ans,s:longint; begin readln(n,k); fillchar(v,siz ...

  4. JavaScript NodeList和Array

    原文引用脚本之家作者:Jeff Wong,谢谢大神提供资源 在Web前端编程中,我们通常会通过document.getElementsByTagName或者document.getElementsBy ...

  5. HAOI2006受欢迎的牛

    求出强联通分量之后判断出度为0的点有几个,有1个就输出这个分量的点的数目,否则输出0: var i,j,n,m,x,y,ans1,ans2,t,cnt,top:longint; head,next,g ...

  6. 【兼容】IE下PNG色差

    IE(包括IE7)中的PNG色差问题http://bbs.blueidea.com/thread-2910513-1-1.html修正IE对PNG颜色显示错误的问题http://www.lizhenw ...

  7. 【jQuery日期处理】两个时间大小的比较

    function checkEndTime(){ var startTime=$("#startTime").val(); var start=new Date(startTime ...

  8. 那些年一起踩过的坑 — Date类型序列化的问题

      坑在哪里?        序列化 和 反序列化 的时候对Date字段的格式设置不一致        例如:将Java bean序列化成Json string的时候 格式为 yyyy-MM-dd 解 ...

  9. SQL Server 2005如何远程连接数据库?

    SQL Server 2005如何远程连接数据库? 方法/步骤   1 在配置工具中的服务和远程连接的外围应用配置器 --->远程连接-->本地连接和远程连接-->同时使用TCP/I ...

  10. JqueryTips小实验,浏览器滚动条不限制

    最近做公司的项目有些地方可能需要一些小提示,于是自己建立项目研究tips.在此之前看到过一些别人写的JqueryTips,于是借鉴了一些别人的经验在此基础上我做出了一些改进. 有的同学可能使用过其他一 ...