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

时间:2021-04-09 20:50:53

原文: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的组成。

  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"];

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

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

cell.accessoryType =UITableViewCellAccessoryDetailDisclosureButton;

cell.textLabel.text  =@"1";

cell.detailTextLabel.text =@"2";

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

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

文字是顶边的。

不过可以改变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"];

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

自定义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

附注:

大家在使用iPhone通讯录时会发现右侧可以按字母检索,使用起来很方便,其实这个功能使用UITableView实现很简单,只要实现数据源协议的一个方法,构建一个分组标题的数组即可实现。数组元素的内容和组标题内容未必完全一致,UITableView是按照数组元素的索引和每组数据索引顺序来定位的而不是按内容查找。

 -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

     NSLog(@"生成组索引");

    return [[UILocalizedIndexedCollation currentCollation] sectionTitles];

 }

效果如下:

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

apple对这个方法是这么说的:

Asks the data source to return the titles for the sections for a table view.

An array of strings that serve as the title of sections in the table view and appear in the index list on the right side of the table view. The table view must be in the plain style (UITableViewStylePlain). For example, for an alphabetized list, you could return an array containing strings “€A”€ through “Z”.

Parameters

所以在使用的是时候注意style是默认的UITableViewStylePlain.

参考:  http://www.cnblogs.com/kenshincui/p/3931948.html

(这篇文章也非常不错,详细,值得细读, 特别是博客的排版,看到自己的就很惭愧啊,慢慢来吧)