原文: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"];

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

附注:

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

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

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

    return [[UILocalizedIndexedCollation currentCollation] sectionTitles];

 }

效果如下:

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

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

介绍TableView非常不错的一篇文章的更多相关文章

  1. DSL 或者说是抽象 或者说是沉淀 ,我看到的不错的一篇文章

    作者:张浩斌 链接:https://www.zhihu.com/question/45552115/answer/99388265 来源:知乎 著作权归作者张浩斌和知乎所有.   ---------- ...

  2. 在知乎上看到 Web Socket这篇文章讲得确实挺好,从头看到尾都非常形象生动,一口气看完,没有半点模糊,非常不错

    在知乎上看到这篇文章讲得确实挺好,从头看到尾都非常形象生动,一口气看完,没有半点模糊,非常不错,所以推荐给大家,非常值得一读. 作者:Ovear链接:https://www.zhihu.com/que ...

  3. logstash快速入门 (这篇文章很不错 ) | 两种方式往logstash传输数据实例:Apache 日志(从文件获取)、Syslog方式

    原文地址:http://www.2cto.com/os/201411/352015.html 原文地址:http://logstash.net/docs/1.4.2/tutorials/getting ...

  4.   PS2: 这篇文章中的图片绘图工具使用的是Dia (sudo apt-get install dia)。据说yEd也很不错。

    SBCL编译过程 - O.Nixie的专栏 - 博客频道 - CSDN.NET PS2: 这篇文章中的图片绘图工具使用的是Dia (sudo apt-get install dia).据说yEd也很不 ...

  5. 如何在一小时内更新100篇文章?-Evernote Sync插件介绍

    上一篇"手把手教你制作微信小程序,开源.免费.快速搞定",已经教会你如何快速制作一个小程序,但作为资讯类小程序,内容不可少,并且还需要及时更新. 但是,如果让你复制粘贴,可能还需要 ...

  6. github使用-知乎的某小姐的一篇文章

    作者:珊姗是个小太阳链接:http://www.zhihu.com/question/20070065/answer/79557687来源:知乎著作权归作者所有,转载请联系作者获得授权. 作为一个文科 ...

  7. [转] 以后再有人问你selenium是什么,你就把这篇文章给他

    本文转自:https://blog.csdn.net/TestingGDR/article/details/81950593 写在最前面:目前自动化测试并不属于新鲜的事物,或者说自动化测试的各种方法论 ...

  8. 三篇文章了解 TiDB 技术内幕——说存储

    数据库.操作系统和编译器并称为三大系统,可以说是整个计算机软件的基石.其中数据库更靠近应用层,是很多业务的支撑.这一领域经过了几十年的发展,不断的有新的进展. 很多人用过数据库,但是很少有人实现过一个 ...

  9. 三篇文章了解 TiDB 技术内幕 - 说存储(转)

    引言 数据库.操作系统和编译器并称为三大系统,可以说是整个计算机软件的基石.其中数据库更靠近应用层,是很多业务的支撑.这一领域经过了几十年的发展,不断的有新的进展. 很多人用过数据库,但是很少有人实现 ...

随机推荐

  1. android 判断程序是首次(第一次)进入

    很多时候,我们需要判断用户是不是第一次进入程序,以决定是不是给用户一些操作提示. 这种功能的实现,说到底还是将数据(一个标志位)存储起来,下次进入程序的时候读取数据进行判断. 我这里只给出一种较简单的 ...

  2. PL/SQL游标使用

    游标是用来处理使用SELECT语句从数据库中检索到的多行记录的工具.借助游标的功能,数据库应用程序可以对一组记录逐个进行处理,每次处理一行. 游标是从数据表中提取出来的数据,以临时表的形式存放在内存中 ...

  3. HDwiki文件上传导致远程代码执行漏洞

    漏洞版本: HDwiki(2011) 漏洞描述: 互动维客开源系统(HDwiki)作为中国第一家拥有自主知识产权的中文维基(Wiki)系统,由互动在线(北京)科技有限公司于2006 年11月28日正式 ...

  4. (经常看看)jdk 设计模式

    在JDK(Java Development Kit)类库中,开发人员使用了大量设计模式,正因为如此,我们可以在不修改JDK源码的前提下开发出自己的应用软件,本文列出了部分JDK中的模式应用实例,有兴趣 ...

  5. [JIT_APP]Activity生命周期相关的7个方法

    先发一张安卓官方文档里面的Activity生命周期图解 下面在对这7个生命周期内相关的方法做一些简单的介绍 OnCreate() 当Activity被创建的时候,会自动运行该方法.该方法做一些初始化动 ...

  6. C语言实现两栈空间共享

    一个同学让我改一段两栈共享的C语言代码,实现进栈.出栈.输出栈里元素的功能. 代码如下: #include <stdio.h> #include <stdlib.h> #def ...

  7. 开源sip server & sip client 和开发库 一览

    http://www.voip-info.org/wiki/view/Open+Source+VOIP+Software http://blog.csdn.net/xuyunzhang/article ...

  8. 2014 ACM-ICPC Asia Anshan Regional Contest(Online Version)

    题目I - Osu! - HDU 5078 题目分析:最水的一道题吧,求两点间的距离和时间差值的最大比值 #include<stdio.h> #include<math.h> ...

  9. 高性能、高流量Java Web站点打造的22条建议

    @http://www.csdn.net/article/2013-12-20/2817861-22-recommendations-for-building-effective-high-traff ...

  10. SQL ID自增列从1开始重新排序 分类: SQL Server 2014-05-19 14:46 652人阅读 评论(0) 收藏

    数据库中把ID自增长重置成1: 一般做法:(太麻烦) 复制表数据->删除原表.新建一张表->粘贴: 新方法: 数据库中:新建查询->复制.粘贴一下代码->修改表名,执行即可(先 ...