设置里面 tableviewcell 分割线textlabel 的位置吗

iOS开发UITableView 之代码自定义cell的步骤
虽然在UITableView中可以直接拖控件的方式进行添加cell,但是这种方式有一个致命的缺点,那就是cell是固定的,而且cell的高度难以更改。在实际的开发中并不能满足我们的需求。比如以下:
wps_clip_image-16424
在这个TableView中每一个cell中有一个显示时间的label,一个显示内容的button,一个显示头像的imageView 并且由于聊天内容的多少 每一个cell的高度都是动态改变的,显然系统提供的cell并不能满足需求!
与此类似的还有微博手机客户端,糗事百科客户端等等
以下是搭建此类UI界面的大概步骤,不涉及网络功能,和存储功能,所有的数据信息用plist文件配置。
wps_clip_image-8661
1.1 根据数据建立数据模型
@interface CLWeiBo : NSObject
@property (nonatomic,copy) NSString *//正文
@property (nonatomic,copy) NSString *//头像
@property (nonatomic,copy) NSString *//昵称
@property (nonatomic,copy) NSString *//图片
@property (nonatomic,assign,getter = isVip) BOOL *//VIP
1.2 提供两个初始化方法
- (instancetype) initWithDict: (NSDictionary *)//对象方法
+ (instancetype) weiboWithDict: (NSDictionary *)//类方法
方法内部实现就不再赘述了!
2.1 根据数据模型建立frame模型
之所以建立frame模型,是为了封装计算cell内各个控件位置的过程。这体现了OOP的思想!
在Frame模型中,各个控件的Frame会根据传递过来的数据模型进行计算设置,所以Frame模型中必须有一个数据模型属性,且计算各个控件的frame的过程应该放在模型数据属性的set方法中,并且在这里计算出cell的高度
@interface CLWeiBoFrame : NSObject
/** &* &头像的Frame 如果成员变量只允许类本身修改,应该把它声明成readonly,体现良好的编码习惯。 &*/
@property (nonatomic, assign, readonly)CGRect iconF;
/** &* &正文的frame &*/
@property (nonatomic, assign, readonly)CGRect textF;
/** &* &VIP图标的frame &*/
@property (nonatomic, assign, readonly)CGRect vipF;
/** &* &昵称的Frame &*/
@property (nonatomic, assign, readonly)CGRect nameF;
/** &* &图片的Frame &*/
@property (nonatomic, assign, readonly)CGRectpictureF;
/** &* &数据模型 &*/
@property (nonatomic, strong) CLWeiBo *
/** &* &cell的高度 &*/
@property (nonatomic, assign, readonly) CGFloat cellH
3.1 新建CLWeiBocell并且继承自UITableViewCell
有frame模型类型的成员属性,用来对cell内的各个控件设置frame 以及数据
因为frame模型中有一个数据模型 所以这里无需再引入数据模型
@property (nonatomic, strong) CLWeiBoFrame *weiboF
并且提供一个类方法,外界调用建立cell的接口,需要传入参数tableView &。传入tableView参数是为了能够取得tableView内的cell队列循环利用cell
+ (instancetype)cellWithTableView:(UITableView*)tableView
{ & & static NSString *ID = @&weibo&; & & CLWeiBoCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; & & if (cell == nil) { & & & & cell = [[CLWeiBoCell &alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; & & } & &
3.2 重写的cell的构造方法 在初始化cell对象的时候调用,在这个方法中添加cell的子控件
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
self = [super initWithStyle:stylereuseIdentifier:reuseIdentifier];
if (self) { & & & &&
// 1.头像 &
//在这里只是声明并添加控件 控件的frame设置以及数据的设置放在frame模型属性的set方法中 在拿到frame模型数据后同时设置cell内子控件的frame和数据 & & & &&
UIImageView *iconView = [[UIImageView alloc]init]; & & & &&
[self.contentView addSubview:iconView]; & & & &&
self.iconView = iconV & & & & & & & & &
// 2.昵称 & & & &&
UILabel *nameView = [[UILabel alloc] init]; & & & &&
nameView.font = MJNameF & & & &&
[self.contentView addSubview:nameView]; & & & &&
self.nameView = nameV & & & & & & & & &
// 3.会员图标 & & & &&
UIImageView *vipView = [[UIImageView alloc]init]; & & & &&
vipView.image = [UIImage imageNamed:@&vip&]; & & & &&
[self.contentView addSubview:vipView]; & & & &&
self.vipView = vipV & & & & & & & & &
// 4.正文 & & & &&
UILabel *textView = [[UILabel alloc] init]; & & & &&
textView.numberOfLines = 0; & & & &&
textView.font = MJTextF & & & &&
[self.contentView addSubview:textView]; & & & &&
self.textView = textV & & & & & & & & &
// 5.配图 & & & &&
UIImageView *pictureView = [[UIImageViewalloc] init]; & & & &&
[self.contentView addSubview:pictureView]; & & & &&
self.pictureView = pictureV & & } & &&
//frame模型数据 变量的set方法 在这里接到外界的frame模型数据的同时设置frame和显示数据
- (void) setCLWeiBoFrame:(MJStatusFrame *)statusFrame
_statusFrame = statusF & & & & &
// 1.设置数据
& & [self settingData]; & & & & &
// 2.设置frame
& & [self settingFrame];
4.新建控制器继承UITableViewController 并且遵守UITableView数据源协议
4.1 在控制器中声明frame模型属性数组
/** &* &存放所有cell的frame模型数据 &*/
@property (nonatomic, strong) NSArray *weiboF
//在weiboFrames的set方法中完成plist文件中的字典数据向模型数据转换的过程
- (NSArray *)statusFrames
if (_weiboframes == nil)&
{ & & & &&
// 初始化 & & & &&
// 1.获得plist的全路径 & & & &&
NSString *path = [[NSBundle mainBundle]pathForResource:@&statuses.plist& ofType:nil]; & & & & & & & & &
// 2.加载数组 & & & &&
NSArray *dictArray = [NSArrayarrayWithContentsOfFile:path]; & & & & & & & & &
// 3.将dictArray里面的所有字典转成模型对象,放到新的数组中 & & & &&
NSMutableArray *weibosFrameArray = [NSMutableArray array]; & & & &&
for (NSDictionary *dict in dictArray) { & & & & & &&
// 3.1.创建CLWeiBo模型对象 & & & & & &&
CLWeiBo *weibo = [CLWeiBo weiboWithDict:dict]; & & & & & & & & & & & & &
// 3.2.创建CLWeiBoFrame模型对象 & & & & & &&
CLWeiBoFrame *weiboFrame = [[CLWeiBoFrame alloc] init]; & & & & & &&
weiboFrame.weibo = & & & & & & & & & & & & &
// 3.2.添加模型对象到数组中
& &[weibosFrameArray addObject:weiboFrame]; & & & &&
} & & & & & & & & &
// 4.赋值 & & & &&
_weiboframes = _weibosframeA & &&
4.2 实现数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return self.statusFrames.
- (UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
// 1.创建cell & &&
MJStatusCell *cell = [MJStatusCellcellWithTableView:tableView]; & & & & &
// 2.在这里已经算好了cell的高度 & &&
cell.statusFrame =self.statusFrames[indexPath.row]; & & & & &
// 3.返回cell & &&
4.3 实现代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
// 取出这行对应的frame模型 & &&
MJStatusFrame *statusFrame =self.statusFrames[indexPath.row]; & &&
return statusFrame.cellH
您对本文章有什么意见或着疑问吗?请到您的关注和建议是我们前行的参考和动力&&
您的浏览器不支持嵌入式框架,或者当前配置为不显示嵌入式框架。怎样用代码设置cell中textlabel位置和大小_百度知道
怎样用代码设置cell中textlabel位置和大小
SetCellPosition 设置表示单元格的行号和列号的 TableLayoutPanelCellPosition。 SetColumn 设置指定子控件的列位置。 SetColumnSpan 设置子控件跨的列数。 SetRow 设置指定子控件的行位置。 SetRowSpan 设置子控件跨的行数。
其他类似问题
为您推荐:
要重写一个类继承UITableViewCell 然后重写layoutSubviews 方法,在此方法里面可以调整textlabel的位置和大小。不过还是建议直接新建一个UILable 然后 cell addSubView:UILable 比较方便
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁1268人阅读
在使用数据库的时候,最重要的可能是数据元素如何构成。使用TableView也不例外,作为内容显示的主体,最重要的就是每一个数据项的显示了。在UITableView里面,显示每一个数据项的元素被称做Cell。当然数据元素之间可能还有分组,这个分组被称做Section。
在之前的那篇文章里,我们已经介绍了如何创建一个Cell,当然,这个是最基本的Cell类型。
苹果给我们提供了各式各样的Cell类型,供我们使用。
如上图所示:原生支持的Cell类型分别有4种。 分别是:
typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
UITableViewCellStyleDefault, // Simple cell with text label and optional image view (behavior of UITableViewCell in iPhoneOS 2.x)
UITableViewCellStyleValue1,
// Left aligned label on left and right aligned label on right with blue text (Used in Settings)
UITableViewCellStyleValue2,
// Right aligned label on left with blue text and left aligned label on right (Used in Phone/Contacts)
UITableViewCellStyleSubtitle // Left aligned label on top and left aligned label on bottom with gray text (Used in iPod).
这四种类型,分别有不同的功用。我下面一一解释:
UITableViewCellStyleDefault:默认的Cell类型,对应上图的第1、2行。这个类型的Cell,有一个标签和一个可选的图片。我们只要指定相关的标签文字属性和图片对象就行了。
就像这样:
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
cell.textLabel.text = [self.dataArray objectAtIndex:indexPath.row];// 设定Label对象文字
[cell.imageView setImage:[UIImage imageNamed:@&image&]];// 设定图片对象属性
cell还有一个属性,叫做detailTextLabel,这个属性是用来展示第二个Label的。
在UITableViewCellStyleValue1里面,textLabel和detailTextLabel分别是向左向右对齐摆放,Apple说他看起来就像在设置程序里面看到的那样。
在UITableViewCellStyleValue2里面,textLabel和detailTextLabel确分别是向右向左对其拜访,Apple说他看起来就像在电话或者通讯录里看到的那样。
还有一个是UITableViewCellStyleSubtitle,他们分别是上下摆放,就想我们在iPod程序里看到的那样。
这就是Cell的几种基本用法。只要把创建类型换一下,就能得到不同类型的Cell。但是别忘记不同类型的Cell要使用不同的CellIdentifier。
这个就涉及到UITableView对Cell的重用机制了。关于重用机制,omegayy在说的很清楚,大家可以去看看。
版权声明:本文为博主原创文章,未经博主允许不得转载。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:55586次
排名:千里之外
原创:19篇
转载:15篇
(1)(4)(2)(11)(16)设置里面 tableviewcell textlabel 的位置吗_百度知道
设置里面 tableviewcell textlabel 的位置吗
我有更好的答案
readonly, retain) UILabel *textLabel@property(nonatomic, readonlytextLabel和 detailTextLabel 是 readonly 所以你不能改变他们。@property(nonatomic
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁当前访客身份:游客 [
少年不识愁滋味,为赋新词强说愁。 而今识尽愁滋味,却道天凉好个秋。
:引用来自“双子座”的评论 引用来自“西昆仑”的...
:引用来自“forloveyou”的评论 我在搭建3.7环境时...
:免费提供App网页自动下载安装所需https服务器!微...
:引用来自“willingseal”的评论 引用来自“湛蓝的...
:引用来自“mhm426”的评论点击一行后,同时标记了...
:楼主干的漂亮~~
:When the window of contain GtkEntry type is p...
:frame怎么把图像放进去?
:引用来自“Eason-cyx”的评论第四步的连线为什么...
今日访问:221
昨日访问:302
本周访问:523
本月访问:5014
所有访问:398002
iOS开发14:UITableView与UITableViewCell
发表于3年前( 14:27)&&
阅读(20666)&|&评论()
0人收藏此文章,
UITableView用来以表格的形式显示数据。关于UITableView,我们应该注意:
(1)UITableView用来显示表格的可见部分,UITableViewCell用来显示表格的一行。
(2)UITableView并不负责存储表格中的数据,而是仅仅存储足够的数据使得可以画出当前可见部分。
(3)UITableView从UITableViewDelegate协议获取配置信息,从UITableViewDataSource协议获得数据信息。
(4)所有的UITableView实现时实际上只有一列,但是我们可以通过向UITableViewCell中添加子视图,使得它看起来有好几列。
(5)UITableView有两种风格:
&&& ① Grouped:每一组看起来像是圆矩形; &&& ② Plain:这是默认风格,可以修改成Indexed风格。 在下边的小例子中,我们将先实现显示一列数据,然后在每行添加图像,之后再看看UITableViewCell的四种分别是什么样的。最后再进行其他操作,比如设置缩进、修改字体大小和行高等。
1、运行Xcode 4.2,新建一个Single View Application,名称为Table Sample:
2、单击ViewController.xib,使用Interface Builder给视图添加一个UITableView控件,并使其覆盖整个视图:
3、选中新添加的UITableView控件,打开Connection Inspector,找到delegate和datasource,从它们右边的圆圈拉线到File's Owner图标:
4、单击ViewController.h,在其中添加代码:
#import &UIKit/UIKit.h&
@interface ViewController : UIViewController&UITableViewDelegate, UITableViewDataSource&
@property (strong, nonatomic) NSArray *listD
5、单击ViewController.m,在其中添加代码:
5.1 在@implementation后面添加代码:
@synthesize listD
5.2 在viewDidLoad方法中添加代码:
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray *array = [[NSArray alloc] initWithObjects:@&Tree&, @&Flower&,
@&Grass&, @&Fence&, @&House&, @&Table&, @&Chair&,
@&Book&, @&Swing& , nil];
self.listData =
5.3 在viewDidUnload方法中添加代码:
- (void)viewDidUnload
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet =
self.listData =
5.4 在@end之前添加代码:
#pragma mark -
#pragma mark Table View Data Source Methods
//返回行数
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [self.listData count];
//新建某一行并返回
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *TableSampleIdentifier = @&TableSampleIdentifier&;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
TableSampleIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:TableSampleIdentifier];
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
上面的第二个方法中,
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: TableSampleIdentifier];
这个语句根据标识符TableSampleIdentifier寻找当前可以重用的UITableViewCell。当某行滑出当前可见区域后,我们重用它所对应的UITableViewCell对象,那么就可以节省内存和时间。
如果执行词语后,cell为nil,那我们再创建一个,并设置去标识符为TableSampleIdentifier:
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableSampleIdentifier];
这里UITableViewCellStyleDefault是表示UITableViewCell风格的常数,除此之外,还有其他风格,后面将会用到。
注意参数(NSIndexPath *)indexPath,它将行号row和部分号section合并了,通过[indexPath row];获取行号。之后给cell设置其文本:
cell.textLabel.text = [listData objectAtIndex: row];
6、运行一下:
7、给每一行添加图片:
7.1 将图片资源添加到工程:拖到工程中,前面的文章有提到。
7.2 在cellForRowAtIndexPath方法的return语句之前添加代码:
UIImage *image = [UIImage imageNamed:@&blue.png&];
cell.imageView.image =
UIImage *highLighedImage = [UIImage imageNamed:@&yellow.png&];
cell.imageView.highlighedImage = highLighedI
7.3 运行,效果如下:
可以看到,每行左边都出现一张图片。当选中某行,其图片改变。
8、设置行的风格:
表示UITableViewCell风格的常量有:
UITableViewCellStyleDefault
UITableViewCellStyleSubtitle
UITableViewCellStyleValue1
UITableViewCellStyleValue2
这几种风格的区别主要体现在Image、Text Label以及Detail Text Label。
为了体现风格,在cellForRowAtIndexPath方法的return语句之前添加代码:
cell.detailTextLabel.text = @&Detail Text&;
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableSampleIdentifier];
中的UITableViewCellStyleDefault依次换成上面提到的四个风格常量,并运行,效果分别如下:
&&&&&&&&&&& UITableViewCellStyleDefault&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &UITableViewCellStyleSubtitle
&&&&&&&&&&&& UITableViewCellStyleValue1&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& UITableViewCellStyleValue2
9、设置缩进:
将所有行的风格改回UITableViewCellStyleDefault,然后在@end之前添加代码如下:
#pragma mark Table Delegate Methods
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
这里将第row行缩进row,如下图所示:
10、操纵行选择:
在@end之前添加代码:
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
if (row%2 == 0) {
return indexP
上面的方法在选择某行之前执行,我们可以在这个方法中添加我们想要的操作。这里,我们实现的是,如果选择的行号(从0开始计)是偶数,则取消选择。
在@end之前添加代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
NSString *rowValue = [listData objectAtIndex:row];
NSString *message = [[NSString alloc] initWithFormat:
@&You selected %@&, rowValue];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@&Row Selected!&
message:message
delegate:nil
cancelButtonTitle:@&Yes I Did&
otherButtonTitles:nil];
[alert show];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
当选择某行之后,就弹出一个Alert,用来显示我们所做的选择。
运行一下,你会发现第0、2等行无法选择。选择奇数行时会弹出提示:
而且关闭提示框后,选择的那行也被取消了选择,用的语句
[tableView deselectRowAtIndexPath:indexPath animated:YES];
11、设置字体大小和表格行高:
11.1 在cellForRowAtIndexPath方法中的return之前添加代码,用于设置字体和大小:
cell.textLabel.font = [UIFont boldSystemFontOfSize:50];
11.2 在@end之前添加代码,用于设置行高:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 70;
运行,看看效果:
1)">1)">1" ng-class="{current:{{currentPage==page}}}" ng-repeat="page in pages"><li class='page' ng-if="(endIndex<li class='page next' ng-if="(currentPage
相关文章阅读

我要回帖

更多关于 自定义tableviewcell 的文章

 

随机推荐