UITableViewCell定制总结

本文深入探讨了UITableView的自定义方法及性能优化技巧,包括如何正确设置cell背景、理解cell的组成部分及其显示逻辑,以及提供了两种自定义cell的方法。此外,还介绍了官方推荐的提高tableView性能的三个关键点。

介绍TableView非常不错的一篇文章:

http://www.cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html 

官方给出的cell的讲解:

https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html

误区:

    if(cell ==nil)

    {

        cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellId] autorelease];

        cell.backgroundColor = [UIColorgreenColor];

    }

   return cell;

这样设置cell的背景通常是不起作用的,纳尼?!淡定,需要了解一下cell的组成。

  1. backgroundView — the entire background of the row (including what looks like theUITableView's background in UITableViewStyleGrouped style tables   整个的行的背景视图
  2. selectedBackgroundView — replaces the backgroundView when the row is selected.        选中cell后的背景视图,替换原有的背景视图
  3. image — a customizable image (not actually a subview) at the left of the cell.一个定制的image位于cell的左侧
  4. accessoryView — a customizable view at the right of the cell.             一个定制的view位于cell的右侧
  5. contentView — a customizable view between the image and the accessoryView(technically, it extends behind the image).
一部分自定义的区域位于contentView(位于image和accessoryView中间),如果没有accessoryView那么contentView则会霸占accessoryView的位置.

contentView是cell的一个子View,要明确这一点!!

(PS:值得注意的是tableView除了可以自定义背景颜色之外,不可以自定义北京,像自定义背景必须把tableView的背景色置为clear,然后定义tableView上一层的view的背景)

A cell object has various parts, which can change depending on the mode of the table view.

官方给的解释说:cell对象有多重组成部分,可以根据tableView的模式而变化. 

只有cell位于UITableViewCellStyleSubtitle模式下。下面的detailTextLabel才会起作用.

而且没有imageView的时候,textLabel和detailTextLabel的未知是不一样的。

        

cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:cellId] autorelease];

cell.accessoryType =UITableViewCellAccessoryDetailDisclosureButton;

        cell.textLabel.text  =@"1";

        cell.detailTextLabel.text =@"2";

        cell.imageView.image = [UIImageimageNamed:@"1"];



        

cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:cellId] autorelease];

cell.accessoryType =UITableViewCellAccessoryDetailDisclosureButton;

        cell.textLabel.text  =@"1";

        cell.detailTextLabel.text =@"2";

//        cell.imageView.image = [UIImage imageNamed:@"1"];


文字是顶边的。

不过可以改变indentationLevel去使得文字不再顶边

        cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;

        cell.accessoryType =UITableViewCellAccessoryDetailDisclosureButton;

        cell.textLabel.text  =@"1safdasfasfsafas";

        cell.detailTextLabel.text =@"2";

        cell.indentationLevel =2;

        cell.indentationWidth =5; //缩进距离为2*5=10 默认的宽度为10...

        cell.imageView.image = [UIImageimageNamed:@"1"];


自定义cell的两种方式:(具体的定制方式,官方都有给出,很详细还有具体代码可以看)

  • Add subviews to a cell’s content view.注意是加view时加到了contentView上面.

  • 1、可以使用tag的方式,放到xib里面加
  • 2、也可以直接采用程序实现,用tag获取到cell里的东西
  • Create a custom subclass of UITableViewCell.

注意:使用xib自定义的时候要 Enter a reuse identifier in the Identifier text field!!!



关于tableView的性能问题(官方给出三点建议):

  • Reuse cells. Object allocation has a performance cost, especially if the allocation has to happen repeatedly over a short period—say, when the user scrolls a table view. If you reuse cells instead of allocating new ones, you greatly enhance table view performance.

  • Avoid relayout of content. When reusing cells with custom subviews, refrain from laying out those subviews each time the table view requests a cell. Lay out the subviews once, when the cell is created.

  • Use opaque subviews. When customizing table view cells, make the subviews of the cell opaque, not transparent.

要重用cell,要避免重复layOut内容,使用不透明的子view




评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值