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(, , , );
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开发-在表单元中添加子视图的更多相关文章
- 关于cell中添加子视图 复用重叠问题的解决方法
问题本质: 因为你要添加的子视图并不是在自定义的cell中实现的,而是根据系统给的UITableViewCell这个类创建的实例,每次进图 cellForRow方法都会创建一个cell,每次都要创 ...
- iOS开发(OC)中的命名规范
开小差:最近发现自己有一个经验主义的毛病,不太容易接受新的知识,这对从事技术研发的人来说不太合理,需要改之. 正文:通过读写大量代码我有自己的一套编程思路和习惯,自认为自己的编码习惯还是不错的,代码结 ...
- IOS开发之表视图(UITableView)
IOS开发之表视图(UITableView)的基本介绍(一) (一):UITableView的基本概念 1.在IOS开发中,表视图的应用十分广泛和普及.因此掌握表视图的用法显得非常重要.一般情况下对于 ...
- iOS开发拓展篇—xib中关于拖拽手势的潜在错误
iOS开发拓展篇—xib中关于拖拽手势的潜在错误 一.错误说明 自定义一个用来封装工具条的类 搭建xib,并添加一个拖拽的手势. 主控制器的代码:加载工具条 封装工具条以及手势拖拽的监听事件 此时运行 ...
- IOS开发效率之为Xcode添加常用的代码片段
IOS开发效率之为Xcode添加常用的代码片段 原文地址:http://blog.csdn.net/pingchangtan367/article/details/30041285 tableview ...
- iOS开发实用技巧—Objective-C中的各种遍历(迭代)方式
iOS开发实用技巧—Objective-C中的各种遍历(迭代)方式 说明: 1)该文简短介绍在iOS开发中遍历字典.数组和集合的几种常见方式. 2)该文对应的代码可以在下面的地址获得:https:// ...
- 解决UIViewController中添加子控制器viewWillAppear不调用问题
问题描述: 我在UICollectionViewController中添加子控制器数组, 并在cellForItem中把子控制器数组中对应的控制器对应的view添加到了UICollectionView ...
- [iOS]在NavigationController中的ScrollView中的子视图都会下移64个像素
情况是这种: 我有一个UINavigationController,设置为self.window的root视图, 然后有一个UIVIewController是UINavigtionController ...
- UIScrollView中添加一个视图,实现让其始终固定在某个位置
ScrollView中添加一个视图,实现让其始终固定在某个位置,如最底部的位置.方法是自定义一个继承UIScrollView,重写它的layoutSubviews方法.代码如下: #import &q ...
随机推荐
- 在安装ISE的情况下,充分利用ISE的安装目录,查找资料
2013-06-22 11:03:02 在找资料时,通过官网输入关键字的方法找资料,有事会给出很多版本的链接.或者找不到,下面给出一种简便的方法,可以快速找到想要的资料. 如果要找ISE各个工具如pl ...
- DSP\BIOS调试Heaps are enabled,but not set correctly
转自:http://blog.sina.com.cn/s/blog_735f291001015t9i.html Heaps are enabled, but the segment for DSP/B ...
- JavaScript DOM高级程序设计 5动态修改样式和层叠样式表1(源代码)--我要坚持到底!
W3C DOM2样式规范 现在这边贴出本章要的源代码,注意要结合前面用到的ADS库http://vdisk.weibo.com/s/Dq8NU CSSStyleSheet对象属性: type :始终是 ...
- <s:property="a" value=""/>取的<s:debug></s:debug>中的value stack中的属性值
<s:property="a" value=""/>取的<s:debug></s:debug>中的value stack中 ...
- JVM问题定位工具
JDB JDB是基于文本和命令行的调试工具,Jikes在JDB的基础上提供了GUI.熟悉JDB还是有价值的,很多情况下需要我们在命令行下完成简单的debug问题定位. 1 2 3 jdb -class ...
- VC++6.0环境下调试c语言代码的方法和步骤_附图
1.C语言程序四步开发步骤 (1)编辑.可以用任何一种编辑软件将在纸上编写好的C语言程序输入计算机,并将C语言源程序文件*.c以纯文本文件形式保存在计算机的磁盘上(不能设置字体.字号等). (2)编译 ...
- Java [leetcode 16] 3Sum Closest
题目描述: Given an array S of n integers, find three integers in S such that the sum is closest to a giv ...
- insert /*+APPEND*/ 各种insert 插入速度比较
SQL> select count(*) from t;COUNT(*)----------5442048****************************SQL> alter ta ...
- TortoiseGit日常使用指南
本文在介绍了软件安装和设置后, 写了TortoiseGit 常用的一些功能, 包括:创建新库添加文件及文件夹创建分支看分支情况及修改log比较版本差异合并分支其他操作: Stash; 忽略文件本文不包 ...
- erlang学习笔记之基础语法
字符串是双引号,单引号的是atom元组: 下标从1开始 X = {'test1',2,3,4}. element(1,X). 配合模式匹配,可以给元素项命名,直接不用下标标记元素项 列表增删改查 增加 ...