【IOS】UITableView样式的自定义

很多时候,我们需要自定义UITableView来满足我们的特殊要求。这时候,关于UITableView和cell的自定义和技巧太多了,就需要不断的总结和归纳。

1.添加自定义的Cell。

这个问题已经涉及过,但是,这里要说的主要是两种方法的比较!

因为,我经常发现有两种方式:

1.xib方式

这种方式,也就是说,为自定义的UITableViewCell类添加一个xib的文件。并且让两者关联。

这时候,写法为:

// 返回cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *CellIdentifier = @"MyCell";

// 自定义cell

MyCell *cell = (MyCell *)[tableVie dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil){

// 这种方式,将会查找响应的xib文件,将不会调用initWithStyle方法

NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:niloptions:nil];

cell = [array objectAtIndex:0];

}

这种方式,是读取了xib文件,所以,就直接按照响应的xib中的布局,布局好了,并不会调用相应的initWithStyle方法。

2.调用initWithStyle方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *CellIdentifier = @"MyCell";

// 自定义cell

MyCell *cell = (MyCell *)[tableVie dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil){

// 这种方式,将会调用cell中的initWithStyle方法

cell = [[[MyCell alloc] initWithStyle:UITableViewCellSelectionStyleGray reuseIdentifier:CellIdentifier] autorelease];

}

return cell;

}

这种方式,会调用相应Cell类的initWithStyle方法。

那么,什么时候,用那种方式呢?

我的理解是:

当,cell比较简单时,可以添加相应的xib文件,进行关联;当cell比较复杂时,就直接用纯代码的方式(不创建相应的xib文件)。

我发现,我还是喜欢用纯代码的方式来写,因为,扩展性好,尤其当cell元素复杂甚至带有动画效果的时候,用xib反而很难控制,或者根本无法控制。

我建议用纯代码的方式!

2.设置cell的setAccessoryView属性

主要用在:在右边添加一个自定义的按钮,或者子视图。

为setAccessoryView设置一个按钮。

cell.accessoryType = UITableViewCellAccessoryNone;

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

[button setFrame:CGRectMake(0.0, 0.0, 55, 57)];

[button setImage:[UIImage imageNamed:@"tap_normal.png"]forState:UIControlStateNormal];

[button setImage:[UIImage imageNamed:@"tap_highlight.png"]forState:UIControlStateHighlighted];

[button setTag:indexPath.row];

[button addTarget:self action:@selector(doClickPlaybillAction:event:) forControlEvents:UIControlEventTouchUpInside];

[button setBackgroundColor:[UIColor clearColor]];

[cell setAccessoryView:button];

return cell;

通过观察属性定义:

@property(nonatomic) UITableViewCellAccessoryType   accessoryType;

@property(nonatomic,retain) UIView                 *accessoryView;

@property(nonatomic) UITableViewCellAccessoryType   editingAccessoryType;

@property(nonatomic,retain) UIView                 *editingAccessoryView;

可见,accessoryView属性需要的参数为UIView,所以,可以很方便的自定义。当然还有editingAccessoryView,可以进行自定义修改时的UIView。

根据用户点击的按钮,找到相应的Cell

 

- (void) performExpand:(id)paramSender{

UITableViewCell *ownerCell = (UITableViewCell*)[paramSender superview];// 获得父视图,即TableViewCell

if (ownerCell != nil){

NSIndexPath *ownerCellIndexPath = [self.myTableView indexPathForCell:ownerCell];

NSLog(@"Accessory in index path is tapped. Index path = %@", ownerCellIndexPath);

}

}

3.自定义cell选择时的样式。

通过,上面一步,我们为Cell添加了一个自定义的按钮。

也许就会遇到这么一个纠结的情况,当点击UITableViewCell高亮时,其子视图中不该高亮的对象(比如说自定义的那个按钮)也高亮了。

比如:

正确方式:我们需要cell被选中时,按钮不应该也被高亮显示。如:

错误方式:但是,cell被选中时,按钮却也高亮显示了。如:

要解决该方法,可以这样:

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{

[super setHighlighted:highlighted animated:animated];

if(highlighted) {

[(UIButton *)self.accessoryView setHighlighted:NO];

}

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated{

[super setSelected:selected animated:animated];

if(selected) {

[(UIButton *)self.accessoryView setHighlighted:NO];

}

}

这样,问题时解决了,那如果我们再深一层次,发问一下:

为什么UITableViewCell被选中时,UITableViewCell中的其他元素也会被高亮显示呢?

因为当UITableViewCell为选中状态时,UITableViewCell把selectedBackgroundView当作一个子视图来添加;

selectedBackgroundView被添加在UITableViewCell的backgroundView之上,或者所有其它视图之下。

当调用setSelected: animated:这一方法时,会导致selectedBackgroundView以一个alpha消化的状态来出现和消失。

还应该注意:

UITableViewCell的selectionStyle值为UITableViewCellSelectionStyleNone时,selectedBackgroundView将不起作用。

4.为UITableViewCell添加自定义背景

有时候,我们要为UITableViewCell自定义的类的每个cell添加自定义的背景图片。

有很多方法:

1.在自定义的UITableViewCell类的initWithStyle方法中,添加如下代码:

// 设置背景

UIImageView *bgImage=[[[UIImageView alloc] initWithFrame:CGRectMake(0, 0,320, 57)] autorelease];

[bgImage setImage: [UIImage imageNamed:@"table_live_bg.png"]];

[self setBackgroundView:bgImage];

2.使用setBackgroundImageByName方法或者setBackgroundImage方法

[self setBackgroundImageByName:@"table_live_bg.png"];

[self setBackgroundImage:[UIImage imageNamed:@"table_live_bg.png"]];

这种方法,要注意的时,设置的图片大小应该与cell大小相同

3.设置cell的contentView,用insertSubview方法

[self.contentView insertSubview:messageBackgroundViewbelowSubview:self.textLabel];

self.selectionStyle = UITableViewCellSelectionStyleNone;

这三种方式,都在initWithStyle方法中设置。

5.设置删除Cell时的自定义文本

//定制Delete字符串,添加函数 返回要显示的字符串

-(NSString *)tableView:(UITableView*)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{

return @"删除";

}

本文转载自:http://blog.sina.com.cn/s/blog_7b9d64af0101aaoy.html

仅供学习参考

时间: 2024-08-19 02:40:34

【IOS】UITableView样式的自定义的相关文章

iOS UITableView通过代码自定义Cell(Cell高度不一定)

1.新建一个继承自UITableView的类 2.重写initWithStyle:reuseIdentifier:方法 添加所有需要显示的子控件(不需要设置子控件的数据 和 frame,并且子控件要添加到self.contentView中). 进行子控件的一次性的属性设置(有些属性只要设置一次,比如字体 和 某些固定的图片) 3.提供两个模型 数据模型:存放 文字数据 和 图片数据 frame模型:存放数据模型,所有子控件的frame,cell的高度 4.cell拥有一个frame模型 (不要直

IOS UITableView Group&Section

UItableView 根据数据结构不同 会有不同样式 关键在两个代理 tableviewdelegate&tabledatasourse 下面代码是我实施的Group 在模拟器中 ios6.1和ios7 并且滚动后相应的section会“置顶”,效果不错哦! 核心代码: #import <UIKit/UIKit.h> @interface AnnouncementViewController : UIViewController<UITableViewDataSource,UI

iOS开发多线程之自定义NSOperation

iOS开发多线程篇—自定义NSOperation 一.实现一个简单的tableView显示效果 实现效果展示: 代码示例(使用以前在主控制器中进行业务处理的方式) 1.新建一个项目,让控制器继承自UITableViewController. 1 // 2 // YYViewController.h 3 // 01-自定义Operation 4 // 5 // Created by apple on 14-6-26. 6 // Copyright (c) 2014年 itcase. All rig

ios UItableView,UITableViewHeaderFooterView分组头部的重用机制,简单地仿射变换CGAffineTransform

怎样设置包括第一栏在内相同高度的section(小技巧,虽然容易但容易忽略) *第一步,在viewdidload里将尾部设为0,table.sectionFooterHeight = 0;(代理方法)- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return 0; }虽然也可以设置尾部高度,但是设置后没有效果 第二步,调用tableView的代理方法- (CGF

iOS开发多线程篇—自定义NSOperation

iOS开发多线程篇—自定义NSOperation 一.实现一个简单的tableView显示效果 实现效果展示: 代码示例(使用以前在主控制器中进行业务处理的方式) 1.新建一个项目,让控制器继承自UITableViewController. 1 // 2 // YYViewController.h 3 // 01-自定义Operation 4 // 5 // Created by apple on 14-6-26. 6 // Copyright (c) 2014年 itcase. All rig

IOS UItableView得group风格如何去掉分割线问题

在自定义UItableView的时候,当选择的style为Group时,往往在设置透明后分割线还在,为了去除,只要重新设置一个BackgroundView覆盖掉原来的即可 //取消分割线 UIView *view= [ [ [ UIView  alloc ] init ] autorelease]; [cell setBackgroundView :view]; //取消点击效果 cell.selectionStyle = UITableViewCellSelectionStyleNone; I

iOS开发UI篇—自定义瀑布流控件(基本实现)

iOS开发UI篇—自定义瀑布流控件(基本实现) 一.基本实现 说明:在View加载的时候,刷新数据. 1.实现代码 YYViewController.m文件 1 // 2 // YYViewController.m 3 // 06-瀑布流 4 // 5 // Created by apple on 14-7-28. 6 // Copyright (c) 2014年 wendingding. All rights reserved. 7 // 8 9 #import "YYViewControll

iOS开发UI篇—自定义瀑布流控件(蘑菇街实现)

iOS开发UI篇—自定义瀑布流控件(蘑菇街瀑布流) 一.简单说明 关于瀑布流 1.是使用UIScrollView实现的 2.刷新数据(reloadData)方法里面做哪些事情 3.layoutSubviews方法里面做哪些事情 4.模仿UItableView进行设计 完善: 瀑布流控件第一次显示到屏幕上的时候自动的向数据源索要数据,而不需要手动调用.这需要监听View的显示,View的显示有一个方法,叫做willMoveToSuperview:在该方法中直接刷新一次数据即可. 二.把自定义的瀑布

iOS UITableView代理方法详解

原 iOS UITableView代理方法详解 IOS UITableView的代理方法详解(http://my.oschina.net/u/2340880/blog/404958) 一.补充 在上一篇博客中,http://my.oschina.net/u/2340880/blog/404605,我将IOS中tableView(表视图)的一些常用方法总结了一下,这篇将tableView的代理方法作了总结,对上一篇博客进行了补充. 二.UITableViewDataSourc(数据源代理) 1.必