设置里面 tableviewcell cell.textlabell 的位置吗

温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
个人认为最好的方式是,// If you're targeting iOS 6, set the label's background color to clear
// This must be done BEFORE changing the layer's backgroundColor
cell.textLabel.backgroundColor = [UIColor clearColor];
// Set layer background color
cell.textLabel.layer.backgroundColor = [UIColor blueColor].CGColor;
阅读(1894)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
在LOFTER的更多文章
loftPermalink:'',
id:'fks_',
blogTitle:'tableviewcell 被选中时,label的背景色被还原成透明',
blogAbstract:'今天遇到了这个问题,&这里有解决方法,/questions/2965085/uitableviewcell-makes-labels-background-clear-when-highlighted/66118个人认为最好的方式是,',
blogTag:'',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:0,
publishTime:7,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:1,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'',
hmcon:'0',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}UItableViewCell 设置不了默认控件 textLabel ,detailTextL... - 简书
UItableViewCell 设置不了默认控件 textLabel ,detailTextLable,ImageView 的frame的问题
系统默认的UItableViewCell好用简便,一个图片(imageView)+一个标题(textLabel)+一个详细信息(detailtextLable) 三个默认控件已足够呈现大部分的简单列表, 但是在我想要调整三个默认控件的frame时,发现在获取cell时,或者willDisplayCell时都更改不了他们的frame.
每次UITabeleView调用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
获取Cell后,经过一系列我们未知的操作后,都会调用每个Cell的layoutSubviews方法来对每个Cell进行布局.
在UItableViewCell的默认layoutSubviews实现里,会调用一个私有的UITableViewCellLayoutManager对象的layoutSubviewsOfCell:方法对该cell里的内容进行实际布局.
void __cdecl -[UITableViewCellLayoutManager layoutSubviewsOfCell:]
接着在layoutSubviewsOfCell里,才会真正的对默认的:imageView textLabel detailTextLabel 三个控件进行frame设置:
(1)对imageView调用sizeToFit来实现imageView的尺寸调,这个过程是通过imageView里的image大小来自动调整的,所以就会有"如果给的图片大小不一样,默认列表各项的imageView大小也参差不齐"这种现象,因为它根据实际image自动计算.
(2)对textLabel和detailTextLabel通过一个:
void __cdecl -[UITableViewCellLayoutManager getTextLabelRect:detailTextLabelRect:forCell:rowWidth:]
的私有方法来计算两个label的frame ,这个方法会根据字体,cell大小等计算各个label的合适rect。
获取frame后接着就会setFrame。
总之一句话概括就是我们所有的frame设置都会在稍后被UITableViewCell 的默认layoutSubview方法给覆盖了。
2.解决方式:覆盖父类的设置
定义UItableViewCell子类,重写layoutSubviews方法。在子类的layoutSubviews方法里设置各个默认控件的frame,覆盖父类的设置:
@interface IpodMusicCell : UITableViewCell
@implementation IpodMusicCell
- (void)layoutSubviews {
//一定要先调 [super layoutSubviews] 才谈得上覆盖父类的设置
[super layoutSubviews];
CGFloat x=0,y=0,w=0,h=0;
h = self.contentView.frame.size.height - 2*5;
self.imageView.frame = CGRectMake(x, y, w, h);
x = self.imageView.frame.origin.x + self.imageView.frame.size.width + 10;
y = self.imageView.frame.origin.y + 5;
w = self.contentView.frame.size.width - x - 8;
self.textLabel.frame = CGRectMake(x, y, w, h);
x = self.textLabel.frame.origin.x;
y = self.imageView.frame.origin.y + self.imageView.frame.size.height -20 - 5;
w = self.textLabel.frame.size.
self.detailTextLabel.frame = CGRectMake(x, y, w, h);
//如果[super layoutSubviews]放在这里,前面的设置又会被父类覆盖
//[super layoutSubviews];
3.调整后效果:
imageView整齐了;各个label的frame也已更改.解决TableViewCell的分割线往右挫15个像素点以及细节问题
我的图书馆
解决TableViewCell的分割线往右挫15个像素点以及细节问题
//以前在使用UITableView的时候,
总是在cell上自己加Label,
遇到cell的accessoryType不同的时候,
需要自己调整Label的大小和位置.
后来发现 UITableViewCell中有textLabel和detailTextLabel可以使用,
系统配置好了大小位置,
可以根据cell的不同Style和大小自动调整.
//textLabel就是放置在cell左边的Label,
detailTextLabel就是放置在cell右边的Label,
使用的时候需要把cell的Style设为UITableViewCellStyleValue1.
//代码如下:
UITableViewCell *cell =[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1reuseIdentifier:@"cell"]autorelease];
[cell.textLabelsetText:@"选项"];
[cell.detailTextLabelsetTextColor:[UIColorcolorWithWhite:0.52alpha:1.0]];
[cell.detailTextLabelsetText:@"详细内容"];
cell.accessoryType =UITableViewCellAccessoryN//cell没有任何的样式
cell.accessoryType =UITableViewCellAccessoryDisclosureI//cell的右边有一个小箭头,距离右边有十几像素;
cell.accessoryType =UITableViewCellAccessoryDetailDisclosureB//cell右边有一个蓝色的圆形button;
cell.accessoryType =UITableViewCellAccessoryC//cell右边的形状是对号;
//改变UITableViewCell选中时背景色
cell.selectedBackgroundView = [[[UIViewalloc]initWithFrame:cell.frame]autorelease];
cell.selectedBackgroundView.backgroundColor =[UIColorredColor];
//这样写在IOS7.0以后 TableViewCell的分割线就不会往右挫15个像素点了
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
[tableViewsetSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
发表评论:
TA的最新馆藏Pages: 1/2
主题 : UITableViewCell的detailTextLabel位置显示图片
级别: 新手上路
UID: 379965
可可豆: 183 CB
威望: 102 点
在线时间: 76(时)
发自: Web Page
UITableViewCell的detailTextLabel位置显示图片&&&
当cell的style设置成UITableViewCellStyleValue1,cell的accessory style设置成UITableViewCellAccessoryDisclosureIndicator的时候,tableViewCell的样式是右边有个小箭头,然后箭头前可以添加DetailTextLabel。请问各位大神,我现在想小箭头左边,也就是detailTextLabel得位置不是显示文本而是显示图片,有办法实现么?方法是这样呢?
级别: 侠客
可可豆: 429 CB
威望: 494 点
在线时间: 1200(时)
发自: Web Page
实例化一个UIImageView,然后加到cell中
[cell.contentview addsubview:yourimageview];
具体的坐标你得自己调了。
级别: 新手上路
UID: 379965
可可豆: 183 CB
威望: 102 点
在线时间: 76(时)
发自: Web Page
大道至简,谢谢你的回答。我还一直在想是否能够把自定义的ImageView添加到DetailTextLabel中去。
级别: 侠客
可可豆: 429 CB
威望: 494 点
在线时间: 1200(时)
发自: Web Page
我累的时候也会经常钻牛角尖 呵呵
级别: 新手上路
UID: 332368
可可豆: 32 CB
威望: 19 点
在线时间: 11(时)
发自: Web Page
创建一个UITableViewCell类,然后自定义xib
&
级别: 新手上路
UID: 379965
可可豆: 183 CB
威望: 102 点
在线时间: 76(时)
发自: Web Page
谢谢你得回答,这也是一个方法,只不过就麻烦了一点,还是很感谢你得回答
级别: 新手上路
可可豆: 84 CB
威望: 74 点
在线时间: 66(时)
发自: Web Page
回 5楼(linseanyu) 的帖子
楼主是不是要这个样子的?在保证detailTextLabel的frame存在的前提下,直接添加就OK了[cell.detailTextLabel addSubview:userHeaderImageView];
图片:4892F16A-D82E-490B-8F4C-0DDD9CC5A555.png
级别: 新手上路
UID: 379965
可可豆: 183 CB
威望: 102 点
在线时间: 76(时)
发自: Web Page
回 6楼(梁通) 的帖子
是的是的,问题已经解决了,就跟你和1楼说的一样,非常感谢你热心的回答
级别: 新手上路
UID: 496337
可可豆: 150 CB
威望: 142 点
在线时间: 330(时)
发自: Web Page
cell.detailTextLabel addSubview:imageView我这个为什么图片没发显示,而按照一楼的cell.contentView addSubview:imageView却可以显示了。
级别: 新手上路
可可豆: 95 CB
威望: 95 点
在线时间: 90(时)
发自: Web Page
回 8楼(Fs_purple) 的帖子
应该是你没有把cell的类型设置为UITableViewCellStyleSubtitle
Pages: 1/2
关注本帖(如果有新回复会站内信通知您)
苹果公司现任CEO是谁?2字 正确答案:库克
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 浏览移动版}

我要回帖

更多关于 cell.textlabel 居中 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信