设置里面自定义tableviewcellltextlabel的位置吗

19537人阅读
& & & & 我们常用的表格类视图就是用&UITableView与UITableViewCell,UITableViewController继承UIViewContoller,所以只要很少代码就可以显示一个视图,UITableViewController也是UIScrollView子类,所以也有上下滑动效果&;UITableView和UITableViewCell不能储存数据,可以用来显示特定行数内的数据,而且,也并不是把所有数据都放在单元格cell视图上,而是通过单元格重用和实现UITableViewDataSource,UITableViewDelegate协议的方法形式显示出来;
1.新建工程名为SampleTable , File-&New-&Project -&single View Application -& next&
2.添加UITableViewDataSource,UITableViewDelegate协议
#import &UIKit/UIKit.h&
@interface STViewController : UIViewController&UITableViewDataSource,UITableViewDelegate&
@property(strong,nonatomic) NSArray *listD
@property(strong,nonatomic)UITableView *tableV
@property(strong,nonatomic)UITableViewCell *tableViewC
@end声明了一个存放数据的数组和用于显示单元格的两个对象
2.在@implementation STViewController后面添加上
@synthesize listData=_listData;
@synthesize tableView = _tableView;
@synthesize tableViewCell =_tableViewCell
viewDidLoad中实现对界面初始化工作,UITableView有两种风格,
UITableViewStylePlain & &默认风格,最常见的
UITableViewStyleGrouped &圆角矩形风格
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//初始化表格
self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
// 设置协议,意思就是UITableView类的方法交给了tabView这个对象,让完去完成表格的一些设置操作
self.tableView.delegate=
self.tableView.dataSource=
//把tabView添加到视图之上
[self.view addSubview:self.tableView];
存放显示在单元格上的数据
NSArray *array = [NSArray arrayWithObjects:@&张三&,@&张四&,@&张五&,@&李三&,@&李四&,@&李五&,@&李六&,@&王三&,@&王四&,@&王五&,@&王六&,@&王七&,@&王八&,@&王九&,@&王十&, nil];
self.listData =
3.视图上显示单元格的内容以及一些数据都是都是属性都是依赖于协议的代理方法
//返回多少个section
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
//返回行数,也就是返回数组中所存储数据,也就是section的元素
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return [self.listData count];
UITableView每一行都有一个UITableViewCell的实例表示,它也继承UIView,也就是每一行又拥有一个子视图,如果是大型表格,这样开销就非常大,所以就有了单元格的重用;当一部分单元格滚出屏幕后,他们被放在一个可重用的单元序列之中。如果系统运行比较慢,表视图就会从序列中删除这些单元,释放空间,如果有储存空间,表视图就会重新获取这些单元,以后面使用;
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
声明静态字符串型对象,用来标记重用单元格
static NSString *TableSampleIdentifier = @&TableSampleIdentifier&;
用TableSampleIdentifier表示需要重用的单元
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableSampleIdentifier];
如果如果没有多余单元,则需要创建新的单元
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:TableSampleIdentifier];
while ([cell.contentView.subviews lastObject ]!=nil) {
[(UIView*)[cell.contentView.subviews lastObject]removeFromSuperview];
获取当前行信息值
NSUInteger row = [indexPath row];
填充行的详细内容
cell.detailTextLabel.text = @&详细内容&;
把数组中的值赋给单元格显示出来
cell.textLabel.text=[self.listData objectAtIndex:row];
cell.textLabel.backgroundColor= [UIColor greenColor];
表视图单元提供的UILabel属性,设置字体大小
cell.textLabel.font = [UIFont boldSystemFontOfSize:40.0f];
tableView.editing=YES;
cell.textLabel.backgroundColor = [UIColor clearColor];
UIView *backgroundView = [[UIView alloc] initWithFrame:cell.frame];
backgroundView.backgroundColor = [UIColor greenColor];
cell.backgroundView=backgroundV
设置单元格UILabel属性背景颜色
cell.textLabel.backgroundColor=[UIColor clearColor];
正常情况下现实的图片
UIImage *image = [UIImage imageNamed:@&2.png&];
cell.imageView.image=
被选中后高亮显示的照片
UIImage *highLightImage = [UIImage imageNamed:@&1.png&];
cell.imageView.highlightedImage = highLightI
注释内容中有这两个设置表视图背景颜色的属性方法,具体了解可以看这个博客&
讲的比较详细
cell.textLabel.backgroundColor= [UIColor greenColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
UIView *backgroundView = [[UIView alloc] initWithFrame:cell.frame];
backgroundView.backgroundColor = [UIColor greenColor];
cell.backgroundView=backgroundV
表示UITableViewCell风格的常量有:
UITableViewCellStyleDefault
UITableViewCellStyleSubtile
UITableViewCellStyleValue1
UITableViewCellStyleValue2
//设置单元格高度
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
return 90;
//设置单元格缩进
-(NSInteger) tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
NSInteger row = [indexPath row];
if (row % 2==0) {
//选中单元格所产生事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
首先是用indexPath获取当前行的内容
NSInteger row = [indexPath row];
从数组中取出当前行内容
NSString *rowValue = [self.listData objectAtIndex:row];
NSString *message = [[NSString alloc]initWithFormat:@&You selected%@&,rowValue];
弹出警告信息
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@&提示&
message:message
delegate:self
cancelButtonTitle:@&OK&
otherButtonTitles: nil];
[alert show];
&- (CGFloat)tableView:(UITableView&*)tableView heightForHeaderInSection:(NSInteger)
&& & & & & & & & &这个方法返回指定的 section的header view 的高度。
& - (CGFloat)tableView:(UITableView&*)tableView heightForFooterInSection:(NSInteger)
&& & & & & & & & &这个方法返回指定的 section的footer view 的高度。
为了增加效果,所以界面显得比较丑陋,附上运行结果截图
附上源代码:
版权声明:本文为博主原创文章,未经博主允许不得转载。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1134695次
积分:11032
积分:11032
排名:第562名
原创:125篇
转载:23篇
评论:447条
51CTO 博客
关注我的微博
我们工作室微博
文章:15篇
阅读:184599
(1)(2)(4)(2)(5)(2)(6)(8)(6)(9)(1)(5)(19)(10)(4)(8)(13)(28)(16)(1)自定义tableviewcell但是给cell里的label赋值后还是nil
[问题点数:40分,结帖人spoonysnail]
自定义tableviewcell但是给cell里的label赋值后还是nil
[问题点数:40分,结帖人spoonysnail]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
相关帖子推荐:
2015年3月 移动开发大版内专家分月排行榜第二2014年8月 移动开发大版内专家分月排行榜第二
2015年6月 移动开发大版内专家分月排行榜第三2014年12月 移动开发大版内专家分月排行榜第三
2015年4月 移动开发大版内专家分月排行榜第二
2015年5月 移动开发大版内专家分月排行榜第三2015年3月 移动开发大版内专家分月排行榜第三2014年10月 移动开发大版内专家分月排行榜第三
2015年3月 移动开发大版内专家分月排行榜第二2014年8月 移动开发大版内专家分月排行榜第二
2015年6月 移动开发大版内专家分月排行榜第三2014年12月 移动开发大版内专家分月排行榜第三
2015年4月 移动开发大版内专家分月排行榜第二
2015年5月 移动开发大版内专家分月排行榜第三2015年3月 移动开发大版内专家分月排行榜第三2014年10月 移动开发大版内专家分月排行榜第三
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。及旗下全部分类
打开微信扫一扫,关注圣才:
sc100xuexi
认证官方微博
认证官方微博
iOS开发中设置UITableViewCell选中时的颜色
发布人:&&发布日期: 11:55&&共2656人浏览
来源:网络 作者:未知
1.系统默认的颜色设置
cell.selectionStyle&=&UITableViewCellSelectionStyleN&&
cell.selectionStyle&=&UITableViewCellSelectionStyleB&&
cell.selectionStyle&=&UITableViewCellSelectionStyleG&&
2.自定义颜色和背景设置
改变UITableViewCell选中时背景色:
UIColor&*color&=&[[UIColoralloc]initWithRed:0.0green:0.0blue:0.0alpha:1];//通过RGB来定义自己的颜色&
cell.selectedBackgroundView&=&[[[UIView&alloc]&initWithFrame:cell.frame]&autorelease];&&
cell.selectedBackgroundView.backgroundColor&=&[UIColor&xxxxxx];&&
3.自定义UITableViewCell选中时背景
cell.selectedBackgroundView&=&[[[UIImageView&alloc]&initWithImage:[UIImage&imageNamed:@&cellart.png&]]&autorelease];&&
还有字体颜色&&
cell.textLabel.highlightedTextColor&=&[UIColor&xxxcolor];&[cell.textLabel&setTextColor:color]&
4.设置tableViewCell间的分割线的颜色
[theTableView&setSeparatorColor:[UIColor&xxxx&]];&
5、设置cell中字体的颜色
Prettyprint代码
&span&style=&color:󙀸&&class=&com&&//&Customize&the&appearance&of&table&view&cells.
&/span&&span&style=&color:�&&class=&pun&&-&/span&&span&style=&color:�&&
class=&pun&&(&/span&&span&style=&color:X&&class=&typ&&UITableViewCell&/span&
&span&style=
&color:�&&class=&pun&&*)&/span&&span&style=&color:�&&class=&pln&&tableView&/span&&span&style=&color:
�&&class=&pun&&:(&/span&&span&style=&color:X&&class=&typ&&UITableView&/span&&span&style=
&color:�&&class=&pun&&*)&/span&&span&style=&color:�&&class=&pln&&tableView&cellForRowAtIndexPath
&/span&&span&style=&color:�&&class=&pun&&:(&/span&&span&style=&color:X&&class=&typ&&NSIndexPath&/span&&span&
style=&color:�&&class=&pun&&*)&/span&&span&style=&color:�&
&class=&pln&&indexPath&
&/span&&span&style=&color:�&&class=&pun&&{&/span&&span&style=&color:&
#000000;&&class=&pln&&&
&&&/span&&span&style=&color:󖶀&&class=&kwd&&if&/span&&span&style=&color:&
#000000;&&class=&pun&&(&/span&&span&style=&color:ᨊ&&class=&lit&&0&/span&
&span&style=&color:&
#000000;&&class=&pun&&==&/span&&span&style=&color:�&&class=&pln&&
&indexPath&/span&&span&style=&color:�&&class=&pun&&.&/span&&span&
style=&color:�&&class=&pln&&row&/span&&span&style=&color:�&&
class=&pun&&)&/span&&span&style=&color:�&&class=&pln&&&
&&&/span&&span&style=&color:�&&class=&pun&&{&/span&&span&style=&color:&
#000000;&&class=&pln&&&
&&&&cell&/span&&span&style=&color:�&&class=&pun&&.&/span&&span&style=&color:
�&&class=&pln&&textLabel&/span&&span&style=&color:�&&class=&pun&&
.&/span&&span&style=&color:�&&class=&pln&&textColor&&/span&&span&style=
&color:�&&class=&pun&&=&/span&&span&style=&color:�&&class=&pun&&
...;&/span&&span&style=&color:�&&class=&pln&&&
&&&&cell&/span&&span&style=&color:�&&class=&pun&&.&/span&&span&style=&color:
�&&class=&pln&&textLabel&/span&&span&style=&color:�&&class=&pun&&.
&/span&&span&style=&color:�&&class=&pln&&highlightedTextColor&&/span&&span
&style=&color:�&&class=&pun&&=&/span&&span&style=&color:�&&
class=&pun&&...;&/span&&span&style=&color:�&&class=&pln&&&
&&&/span&&span&style=&color:�&&class=&pun&&}&/span&&span&style=&color:&
#000000;&&class=&pln&&&
&&&/span&&span&style=&color:�&&class=&pun&&...&/span&&span&style=&color:&
#000000;&&class=&pun&&}&/span&&
e书题库免费下载
网授课程免费试听cell中使用自定义label
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { &
& & static NSString *identfy = @&skinCell&; &
& & UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identfy]; &
& & if (cell == nil) { &
& & & & &&
& & & & cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identfy]; &
& & & & &&
// & & & &自定义label &
& & & & UILabel *textLabel = [UIFactory createLabel:kSkinListLabel]; &
& & & & textLabel.frame = CGRectMake(10, 10, 200, 30); &
& & & & textLabel.font = [UIFont boldSystemFontOfSize:16.0f]; &
& & & & textLabel.backgroundColor = [UIColor clearColor]; &
& & & & textLabel.tag = 111; &
& & & & [cell.contentView addSubview:textLabel]; &
& & UILabel *label = (UILabel *)[cell.contentView viewWithTag:111]; &
& & label.text = skinArray[indexPath.row]; &
// & &cell.textLabel.text = skinArray[indexPath.row]; &
-------------------------------
您对本文章有什么意见或着疑问吗?请到您的关注和建议是我们前行的参考和动力&&
您的浏览器不支持嵌入式框架,或者当前配置为不显示嵌入式框架。当前访客身份:游客 [
当前位置:
本人是刚转iOS开发的菜鸟,在做demo的过程中遇到了一点系列问题,求大神一解疑惑。
这是简单的代码:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
if (_current == indexPath.row) {
return 90;
return 50;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
if (cell ==nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
// Configure the cell...
if (_current == indexPath.row) {
cell.textLabel.text = @"中国国足";
CGRect rect = cell.
rect.origin.y +=20;
UILabel *label =
[[UILabel alloc]initWithFrame:rect];
label.text=@"hello";
[cell addSubview:label];
cell.contentView.backgroundColor = [UIColor redColor];
cell.textLabel.text = @"进球了";
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
_current = indexPath.
[tableView reloadData];
大概效果是这样的。
原因我自己知道,就是再cell reuse的时候,重用了之前设置的cell的格式,
if (_current == indexPath.row) {
cell.textLabel.text = @"中国国足";
CGRect rect = cell.
rect.origin.y +=20;
UILabel *label =
[[UILabel alloc]initWithFrame:rect];
label.text=@"hello";
[cell addSubview:label];
cell.contentView.backgroundColor = [UIColor redColor];
cell.textLabel.text = @"进球了";
要是我再上面这段的else中remove掉上面的设颜色,和加label的操作就可以了,但是,这还是最简单的情况,当我用自定义的cell的时候,上面有很多控件,还有按钮,按钮按了可能会加一个view在下面,label的宽度和位置也可能变,那我应该怎么办,在else里面我不可能一条一条撤销原来的操作啊。机智的我又想到了,可以自定义几个不同的cell xib,根据不同的按钮又不同的cell用,但是!!,我又想到了,当不同的按钮现实的view可能会有不同组合的情况,我不可能用那么做那么多xib啊,我只能在cellforrowatindex里面做啊,机智的我又想到了,不用cell重用机制,但是作为又抱负的码农,我在想当我很多条数据,不用重用机制会很悲剧,内存消耗太大啊,怎么办啊啊。
各位大神,求解决啊~怎样兼顾好cell的重用,又能应付各种cell改变的情况呢。
共有1个答案
<span class="a_vote_num" id="a_vote_num_
& &&for (UIView *view in cell.contentView.subviews) {
& & & & [view removeFromSuperview];
if(_current == indexPath.row) {
&&&&&&&&cell.textLabel.text = @"中国国足";
&&&&&&&&CGRect rect = cell.
&&&&&&&&rect.origin.y +=20;
&&&&&&&&UILabel *label =& [[UILabel alloc]initWithFrame:rect];
&&&&&&&&label.text=@"hello";
&&&&&&&&[cell addSubview:label];
&&&&&&&&&cell.contentView.backgroundColor = [UIColor redColor];
&&&&}else{
&&&&&&&&cell.textLabel.text = @"进球了";
--- 共有 2 条评论 ---
按你这种方法的话,只能在cell中加层透明的view覆盖整个cell,然后再这层透明的cell中添加subview,再用你那种方法移掉这个 透明层,透明层上面的东西就自然也移除了,挺好的方法的,但是感觉方法还是不够优雅啊。
(6个月前)&nbsp&
这个removeFromSuperview会移除了之前在自定义cell中的每个cell都该有的东西的啊。
(6个月前)&nbsp&
有什么技术问题吗?

我要回帖

更多关于 tableviewcell 的文章

 

随机推荐