#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. Why it is good practice to declare loggers private, static, and final?

    // Jakarta Commons Loggingprivate static final Log log = LogFactory.getLog(MyClass.class);The above ...

  2. MySQL复制应用中继日志解析

    前言:SQL线程应用中继日志,在binlog_format是row格式的时候,是居于主键更新,下面结合一张图来证明 1.从一个大神那边得到一张图片,SQL线程应用中继日志流程,下面就实验验证一下:(P ...

  3. 获取html上元素的真正坐标

    使用HTML元素的style.left,style.top,style.width,style.height以及width,height属性,都不能获得元素的真正位置与大小,这些属性取出来的都是原来的 ...

  4. 1316. Electronic Auction(树状数组)

    1316 我想说 要不要这么坑 WA了一个小时啊 ,都快交疯了,拿着题解的代码交都WA 最后很无语的觉得题解都错了 重读了N遍题意 发现没读错啊 难道写题解的那个人和我都想错了?? 最后把g++换个C ...

  5. poj1989

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

  6. bzoj1058: [ZJOI2007]报表统计

    set.操作:insert(u,v)在u后面插入v,若u后面已插入过,在插入过的后面插入.mingap求出序列两两之间差值的最小值.minsortgap求出排序后的序列两两之间的最小值.用multis ...

  7. tomcat+nginx+redis实现均衡负载、session共享(一)

    在项目运营时,我们都会遇到一个问题,项目需要更新时,我们可能需先暂时关闭下服务器来更新.但这可能会出现一些状况: 1.用户还在操作,被强迫终止了(我们可以看日志等没人操作的时候更新,但总可能会有万一) ...

  8. linux下判断网络是否连接

    本文改写自网上的一个程序,原始程序中为阻塞式调用,而且有现成创建的过程,非常不利于集成到自己程序中,因此对原始程序进行改造,使其可以完成发送一个imcp包的方式来判断网络连通,只需要调用改进后的 bo ...

  9. Tomcat安全

    一.版本安全 升级当前的tomcat版本为最新稳定版本.故名思议,最新稳定版本就要兼顾最新和稳定这两个概念.一个稳定的版本,是需要时间沉淀的,而最新又是相对于稳定版而言的最新.因此我们一般会选择当前大 ...

  10. 使用struts的同步令牌避免form的重复提交

    struts1避免重复提交 一.使用方法 1.  假如你要提交的页面为toSubmit.jsp: 2.  在打开toSubmit.jsp的Action1中加入:saveToken(request),例 ...