很多时候,我们需要自定义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:nil options: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:messageBackgroundView
belowSubview:self.textLabel];

self.selectionStyle =
UITableViewCellSelectionStyleNone;

 
这三种方式,都在initWithStyle方法中设置。
 
5.设置删除Cell时的自定义文本
 

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

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

return @"删除";

}

 

UITabelViewCell自定义(zhuan)的更多相关文章

  1. ios之UITabelViewCell的自定义(xib实现2)

    上篇文章介绍了如何用UITableView显示表格,并讲了几种UITableViewCell的风格.不过有时候我们需要自己定义 UITableViewCell的风格,其实就是向行中添加子视图.添加子视 ...

  2. ios之UITabelViewCell的自定义(xib实现)

    通过继承UITableViewCell来自定义cell 1.创建一个空的项目.命名: 2.创建一个UITableViewController 并且同时创建xib: 3.设置AppDelegate.m中 ...

  3. ios之UITabelViewCell的自定义(代码实现)

    在用到UITableVIew的时候,经常会自定义每行的Cell 在IOS控件UITableView详解中的下面代码修改部分代码就可以实现自定义的Cell了 [cpp] view plaincopy - ...

  4. UITabelView 高级(自定义Cell)

    自定义一个Cell 当我们要显示复杂数据的时候,例如要做一个扣扣聊天界面,或是新闻列表,系统的行已经不能满足我们的要求,这个时候我们可以通过自定义这个行,让他显示更多复杂结构的样式. 自定义cell就 ...

  5. vue通过自定义指令 v-py 将名字转拼音

    自定义指令 py: 1.新建 vue-py.js文件 import Vue from 'vue'; var chinesePointCode = { "a": [21834, 38 ...

  6. MySQL从删库到跑路_高级(二)——自定义函数

    作者:天山老妖S 链接:http://blog.51cto.com/9291927 一.自定义函数简介 自定义函数(user-defined function UDF)是一种对MySQL扩展的途径,其 ...

  7. 关于Unity3D自定义编辑器的学习

    被人物编辑器折腾了一个月,最终还是交了点成品上去(还要很多优化都还么做).  刚接手这项工作时觉得没概念,没想法,不知道.后来就去看<<Unity5.X从入门到精通>>中有关于 ...

  8. 一起学微软Power BI系列-使用技巧(5)自定义PowerBI时间日期表

    1.日期函数表作用 经常使用Excel或者PowerBI,Power Pivot做报表,时间日期是一个重要的纬度,加上做一些钻取,时间日期函数表不可避免.所以今天就给大家分享一个自定义的做日期表的方法 ...

  9. JavaScript自定义浏览器滚动条兼容IE、 火狐和chrome

    今天为大家分享一下我自己制作的浏览器滚动条,我们知道用css来自定义滚动条也是挺好的方式,css虽然能够改变chrome浏览器的滚动条样式可以自定义,css也能够改变IE浏览器滚动条的颜色.但是css ...

随机推荐

  1. [Android Studio Problems]记录克隆项目中遇到的坑(问题)以及解决方法

    ①Migrate project to Gradle? 问题描述: This project does not use the Gradle build system. We recommend th ...

  2. [Interview]读懂面试问题,在面试官面前变被动为主动

    面试是供需双方心理的较量,作为求职者来说,了解对方问题的内涵,做到“明明白白他的心”,就能变被动为主动.因此,读懂面试问题,掌握面试考官的提问的目的,有准备.有针对性地回答,对提高应聘的成功率是有很大 ...

  3. POP3、IMAP、SMTP邮件协议的理解

    一个热爱技术的菜鸟...用点滴的积累铸就明日的达人 CSDN博客链接: http://blog.csdn.net/my_confesser    正文   今天入职配置OutLook的时候,看到公司的 ...

  4. redis系列文章

    http://blog.csdn.net/liubenlong007/article/details/53690103

  5. delphi杀进程的两种方式

    delphi杀进程的两种方式 uint unit Tlhelp32; 第一种:比较简单,根据标题,找到窗口,再找到进程,杀死进程 procedure KillProgram(WindowTitle : ...

  6. MySql优化--使用索引优化

    原文:http://blog.csdn.net/zuoanyinxiang/article/details/50606837 1.索引优化的原理   在没有使用索引的时候,数据库系统会根据要查找的值到 ...

  7. 【Todo】Apache-Commons-Pool及对象池学习

    有这篇文章: http://www.cnblogs.com/tommyli/p/3510095.html 方案提供了三种类型的pool,分别是GenericKeyedObjectPool,SoftRe ...

  8. php解耦的三种境界

    我们有三个类,Db,FileSystem,Session;实际业务需求要组合操作这三个类. 一.常规做法 class Db { public function read($id) { } } clas ...

  9. Unity异常警告错误处理方法

    原地址:http://www.haogongju.net/art/2591936 1.  The AnimationClip 'cube1_anim' used by the Animation co ...

  10. jQuery unbind() 方法

    jQuery 中的 unbind() 方法是 bind() 方法的反向操作,从每一个匹配的元素中删除绑定的事件. 语法结构: unbind([type][, data]); type是事件类型,dat ...