标签:
原文:http://blog.csdn.net/fanxiaochuan/article/details/11332775
介绍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的组成。
backgroundView
— the entire background of the row (including what looks like theUITableView
‘s background in UITableViewStyleGrouped
style
tables 整个的行的背景视图selectedBackgroundView
— replaces the backgroundView
when the row is selected.
选中cell后的背景视图,替换原有的背景视图image
— a customizable image (not actually a subview) at the left of the cell.一个定制的image位于cell的左侧accessoryView
— a customizable view at the right of the cell. 一个定制的view位于cell的右侧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才会起作用.
detailTextLabel
—A
label for the subtitle if there is additional detail (a UILabel
object)
imageView
—An
image view for an image (a UIImageView
object)
而且没有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上面.
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
标签:
原文地址:http://www.cnblogs.com/A--G/p/4777790.html