设置里面 tableviewcell cell.textlabel 宽度的位置吗

您所在的位置: &
iOS开发UITableViewCell的选中时的颜色设置
iOS开发UITableViewCell的选中时的颜色设置
本文为大家介绍了iOS开发UITableViewCell的选中时的颜色设置,其中包括系统默认的颜色设置,自定义颜色和背景设置,自定义UITableViewCell选中时背景,设置tableViewCell间的分割线的颜色,设置cell中字体的颜色等等内容。
1.系统默认的颜色设置
&cell.selectionStyle&=&UITableViewCellSelectionStyleN&&&&cell.selectionStyle&=&UITableViewCellSelectionStyleB&&&&cell.selectionStyle&=&UITableViewCellSelectionStyleG&&
2.自定义颜色和背景设置
改变UITableViewCell选中时背景色:
UIColor&*color&=&[[UIColoralloc]initWithRed:0.0green:0.0blue:0.0alpha:1];&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&&&&/span&&span&style=&color:�&&class=&pun&&{&/span&&span&style=&color:�&&class=&pln&&&&&&/span&&span&style=&color:󖶀&&class=&kwd&&if&/span&&span&style=&color:�&&class=&pun&&(&/span&&span&style=&color:ᨊ&&class=&lit&&0&/span&&span&style=&color:�&&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:�&&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:�&&class=&pln&&&&&&/span&&span&style=&color:�&&class=&pun&&...&/span&&span&style=&color:�&&class=&pun&&}&/span&&
【编辑推荐】【责任编辑: TEL:(010)】
关于&&的更多文章
Android 4.4即将发布,这对大家来说都是很期待的,当然,无论是
既然强大的Android Studio来了,有什么理由不去用呢?
在iOS开发过程中,尤其是对于新手来说,都会遇到或多
Web App开发中会面临越来越“重”的问题,如果在开始
作为Android开发者,最头疼是什么?相信大家会异口同
本书讲述怎样把UNIX环境下的应用程序移植到Linux环境上运行,是一本综合的开发和解决问题的参考手册 。本书详细描述了当前IT行业
Windows Phone专家
Android开发专家
51CTO旗下网站> tableview 自定义cell 点击cell改变cell中的label.text的字体颜色,ce
tableview 自定义cell 点击cell改变cell中的label.text的字体颜色,ce
发布时间: & &
浏览:1 & &
回复:0 & &
悬赏:0.0希赛币
如题,在tableview里,自定义了cell&,当点击cell后改变cell中的label.text的字体颜色(通过didSelectRowAtIndexPath:方法改变cell中的label.text的颜色)但是,cell在复用的时候,有可能下面的cell用了缓存池里的cell,恰好cell是颜色是之前被改过的。有没有其他的好办法,解决这个问题?
本问题标题:
本问题地址:
温馨提示:本问题已经解决,不能解答。
暂无合适的专家
&&&&&&&&&&&&&&&
希赛网 版权所有 & &&TableView自定义header和footer-Swift基础教程 - swift迷
在TableView中可以自定义Header和footer,在这篇教程中将使用一些国家名称数据填充tableview并且通过自定义UIVIews来创建Header和footer。这篇教程在iOS8.1和Xcode6.1编译通过。
打开Xcode,新建项目选择Single View Application,Product Name填写IOS8SwiftHeaderFooterTutorial,Organization Name和Organization Identifier自行填写,选择与iPhone设备。
移除默认的ViewController同时拖拽TableViewController至Storyboard上,选中TableViewController嵌入NavigationController并设置NavigationController为初始ViewController。双击TableViewController上的Navigation Bar设置Title为"Countries".选中TableViewCell然后到Attributes Inspector面板设置Identifier为"Cell"。
删除ViewController.swift文件并选择“iOS->Source->Cocoa Touch Class”新建继承UITableViewController的TableViewController类
打开Storyboard选中 Table View Controller然后到属性面板设置对象的自定义Class为为刚才创建的“TableViewController”。如下图
打开TableViewController.swift创建如下属性
var countriesinEurope = ["France","Spain","Germany"]
var countriesinAsia = ["Japan","China","India"]
var countriesInSouthAmerica = ["Argentia","Brasil","Chile"]
实现对应的delegate方法:
override func numberOfSectionsInTableView(tableView: UITableView) -& Int {
// Return the number of sections.
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -& Int {
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -& UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
// Configure the cell...
switch (indexPath.section) {
cell.textLabel?.text = countriesinEurope[indexPath.row]
cell.textLabel?.text = countriesinAsia[indexPath.row]
cell.textLabel?.text = countriesInSouthAmerica[indexPath.row]
//return sectionHeaderView
cell.textLabel?.text = "Other"
return cell
1.设置TableView有3个Section
2.设置每个Section有3行数据
3.赋值每行Cell的显示数据
编译并运行项目,界面将大致如下:
接下来我们将会给每个Section增加header和footer.这步可以通过代码或者Interface Builder完成,现在我们将通过 Interface Builder来创建header.在项目增加继承于UITableViewCell的名为CustomHeaderCell类。
打开Storyboard选择TableView.然后打开 Attribute Inspector改变Prototype Cells数为2.
选中第二个Prototype Cell打开Identity Inspector面板设置prototype Cell的自定义class为CustomHeaderCell
打开Attribute Inspector并设置Identifer为HeaderCell
拖拽Label控件至Prototype Cell上并设置水平上下居中,打开Assistant Editor同时确保 CustomHeaderCell.swift可见,Ctrl+Drag方式给Label创建如下Outlet
打开TableViewController.swift实现如下代码
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -& UIView? {
headerCell = tableView.dequeueReusableCellWithIdentifier("HeaderCell") as CustomHeaderCell
headerCell.backgroundColor = UIColor.cyanColor()
switch (section) {
headerCell.headerLabel.text = "Europe";
//return sectionHeaderView
headerCell.headerLabel.text = "Asia";
//return sectionHeaderView
headerCell.headerLabel.text = "South America";
//return sectionHeaderView
headerCell.headerLabel.text = "Other";
return headerCell
现在每个Section都创建了CustomHeaderCell同时Label赋值对应的值,接着我们通过代码方式创建section的footer,如下:
override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -& UIView? {
let footerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40))
footerView.backgroundColor = UIColor.blackColor()
return footerView
override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -& CGFloat {
return 40.0
上面代码创建一个高度为40背景颜色为黑色的View。在tableView:heightForFooterInSection设置高度。编译运行项目,header和footer都会显示出来,效果如下
feiin 于 2 年前
Swift开发交流QQ群:iOS开发15:自定义UITableViewCell-iOS开发小组-好知网
1424个话题
创建时间:
31579 次查看
上篇文章介绍了如何用UITableView显示表格,并讲了几种UITableViewCell的风格。不过有时候我们需要自己定义 UITableViewCell的风格,其实就是向行中添加子视图。添加子视图的方法主要有两种:使用代码以及从.xib文件加载。当然后一种方法比较直 观。
我们这次要自定义一个Cell,使得它像QQ好友列表的一行一样:左边是一张图片,图片的右边是三行标签:
当然,我们不会搞得这么复杂,只是有点意思就行。
1、运行Xcode 4.2,新建一个Single View Application,名称为Custom Cell:
2、将图片资源导入到工程。为此,我找了14张50×50的.png图片,名称依次是1、2、……、14,放在一个名为Images的文件夹中。将此文件夹拖到工程中,在弹出的窗口中选中Copy items into…
添加完成后,工程目录如下:
3、创建一个UITableViewCell的子类:选中Custom Cell目录,依次选择File — New — New File,在弹出的窗口,左边选择Cocoa Touch,右边选择Objective-C class:
单击Next,输入类名CustomCell,Subclass of选择UITableViewCell:
之后选择Next和Create,就建立了两个文件:CustomCell.h和CustomCell.m。
4、创建CustomCell.xib:依次选择File — New — New File,在弹出的窗口,左边选择User Interface,右边选择Empty:
单击Next,选择iPhone,再单击Next,输入名称为CustomCell,选择好位置:
单击Create,这样就创建了CustomCell.xib。
5、打开CustomCell.xib,拖一个Table View Cell控件到面板上:
选中新加的控件,打开Identity Inspector,选择Class为CustomCell;然后打开Size Inspector,调整高度为60。
6、向新加的Table View Cell添加控件:拖放一个ImageView控件到左边,并设置大小为50×50。然后在ImageView右边添加三个Label,设置标签字号,最上边的是14,其余两个是12:
接下来向CustomCell.h添加Outlet映射,将ImageView与三个Label建立映射,名称分别为imageView、nameLabel、decLabel以及locLable,分别表示头像、昵称、个性签名,地点。
选中Table View Cell,打开Attribute Inspector,将Identifier设置为CustomCellIdentifier:
为了充分使用这些标签,还要自己创建一些数据,存在plist文件中,后边会做。
7、打开CustomCell.h,添加属性:
@property (copy, nonatomic) UIImage *
@property (copy, nonatomic) NSString *
@property (copy, nonatomic) NSString *
@property (copy, nonatomic) NSString *
8、打开CustomCell.m,向其中添加代码:
8.1 在@implementation下面添加代码:
8.2 在@end之前添加代码:
- (void)setImage:(UIImage *)img {
if (![img isEqual:image]) {
image = [img copy];
self.imageView.image =
-(void)setName:(NSString *)n {
if (![n isEqualToString:name]) {
name = [n copy];
self.nameLabel.text =
-(void)setDec:(NSString *)d {
if (![d isEqualToString:dec]) {
dec = [d copy];
self.decLabel.text =
-(void)setLoc:(NSString *)l {
if (![l isEqualToString:loc]) {
loc = [l copy];
self.locLabel.text =
这相当于重写了各个set函数,从而当执行赋值操作时,会执行我们自己写的函数。
好了,现在自己定义的Cell已经可以使用了。
不过在此之前,我们先新建一个plist,用于存储想要显示的数据。建立plist文件的方法前面的文章有提到。我们建好一个friendsInfo.plist,往其中添加数据如下:
注意每个节点类型选择。
9、打开ViewController.xib,拖一个Table View到视图上,并将Delegate和DataSource都指向File’ Owner,就像上一篇文章介绍的一样。
10、打开ViewController.h,向其中添加代码:
#import &UIKit/UIKit.h&
@interface ViewController : UIViewController&UITableViewDelegate, UITableViewDataSource&
@property (strong, nonatomic) NSArray *dataL
@property (strong, nonatomic) NSArray *imageL
11、打开ViewController.m,添加代码:
11.1 在首部添加:
#import "CustomCell.h"
11.2 在@implementation后面添加代码:
@synthesize dataL
@synthesize imageL
11.3 在viewDidLoad方法中添加代码:
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//加载plist文件的数据和图片
NSBundle *bundle = [NSBundle mainBundle];
NSURL *plistURL = [bundle URLForResource:@"friendsInfo" withExtension:@"plist"];
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfURL:plistURL];
NSMutableArray *tmpDataArray = [[NSMutableArray alloc] init];
NSMutableArray *tmpImageArray = [[NSMutableArray alloc] init];
for (int i=0; i&[dictionary count]; i++) {
NSString *key = [[NSString alloc] initWithFormat:@"%i", i+1];
NSDictionary *tmpDic = [dictionary objectForKey:key];
[tmpDataArray addObject:tmpDic];
NSString *imageUrl = [[NSString alloc] initWithFormat:@"%i.png", i+1];
UIImage *image = [UIImage imageNamed:imageUrl];
[tmpImageArray addObject:image];
self.dataList = [tmpDataArray copy];
self.imageList = [tmpImageArray copy];
11.4 在ViewDidUnload方法中添加代码:
self.dataList =
self.imageList =
11.5 在@end之前添加代码:
#pragma mark -
#pragma mark Table Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.dataList count];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CustomCellIdentifier = @"CustomCellIdentifier";
static BOOL nibsRegistered = NO;
if (!nibsRegistered) {
UINib *nib = [UINib nibWithNibName:@"CustomCell" bundle:nil];
[tableView registerNib:nib forCellReuseIdentifier:CustomCellIdentifier];
nibsRegistered = YES;
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
NSUInteger row = [indexPath row];
NSDictionary *rowData = [self.dataList objectAtIndex:row];
cell.name = [rowData objectForKey:@"name"];
cell.dec = [rowData objectForKey:@"dec"];
cell.loc = [rowData objectForKey:@"loc"];
cell.image = [imageList objectAtIndex:row];
#pragma mark Table Delegate Methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 60.0;
- (NSIndexPath *)tableView:(UITableView *)tableView
willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
12、运行:
我觉得添加一点必要的讲解会更好
感谢LZ分享。。
讲的很详细。。。
我觉得跟appcoda的教程很相似:
/ios-programming-course/
但是没有appcoda讲得细致,代码贴的多了点。
(大小不能超过2MB,文件类型支持png ,jpg ,gif ,doc ,xls ,txt ,rar ,zip .)
Powered by
扫一扫 关注好知微信下次自动登录
现在的位置:
& 综合 & 正文
iPhone中TableViewCell自动换行的实现
//计算行的高度.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
NSString* cellText = [self.array objectAtIndex:indexPath.row];
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0];
CGSize constraintSize = CGSizeMake(240.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + 20;
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier];
NSInteger row = [indexPath row];
cell.textLabel.text = [self.array objectAtIndex:row];
//自动换行,这里最重要
cell.textLabel.numberOfLines = 0;
//Cell中的小箭头
cell.accessoryType = UITableViewCellAccessoryDisclosureI
其中array为NSObject* 类型的存放NSString集合的对象。
&&&&推荐文章:
【上篇】【下篇】}

我要回帖

更多关于 cell.detailtextlabel 的文章

更多推荐

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

点击添加站长微信