iOS tableView 控件用法 [1] UITableCell的基本用法

来源:互联网 发布:win7 网络 0对象 编辑:程序博客网 时间:2024/04/30 00:07


在使用数据库的时候,最重要的可能是数据元素如何构成。使用TableView也不例外,作为内容显示的主体,最重要的就是每一个数据项的显示了。在UITableView里面,显示每一个数据项的元素被称做Cell。当然数据元素之间可能还有分组,这个分组被称做Section。

在之前的那篇文章里,我们已经介绍了如何创建一个Cell,当然,这个是最基本的Cell类型。

苹果给我们提供了各式各样的Cell类型,供我们使用。


上图所示:原生支持的Cell类型分别有4种。 分别是:

typedef NS_ENUM(NSInteger, UITableViewCellStyle) {    UITableViewCellStyleDefault,// Simple cell with text label and optional image view (behavior of UITableViewCell in iPhoneOS 2.x)    UITableViewCellStyleValue1,// Left aligned label on left and right aligned label on right with blue text (Used in Settings)    UITableViewCellStyleValue2,// Right aligned label on left with blue text and left aligned label on right (Used in Phone/Contacts)    UITableViewCellStyleSubtitle// Left aligned label on top and left aligned label on bottom with gray text (Used in iPod).};

这四种类型,分别有不同的功用。我下面一一解释:

UITableViewCellStyleDefault:默认的Cell类型,对应上图的第1、2行。这个类型的Cell,有一个标签和一个可选的图片。我们只要指定相关的标签文字属性和图片对象就行了。

就像这样:

            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault                                          reuseIdentifier:CellIdentifier];            cell.textLabel.text = [self.dataArray objectAtIndex:indexPath.row];// 设定Label对象文字            [cell.imageView setImage:[UIImage imageNamed:@"image"]];// 设定图片对象属性

cell还有一个属性,叫做detailTextLabel,这个属性是用来展示第二个Label的。

在UITableViewCellStyleValue1里面,textLabel和detailTextLabel分别是向左向右对齐摆放,Apple说他看起来就像在设置程序里面看到的那样。

UITableViewCellStyleValue2里面,textLabel和detailTextLabel确分别是向右向左对其拜访,Apple说他看起来就像在电话或者通讯录里看到的那样。

还有一个是UITableViewCellStyleSubtitle,他们分别是上下摆放,就想我们在iPod程序里看到的那样。

这就是Cell的几种基本用法。只要把创建类型换一下,就能得到不同类型的Cell。但是别忘记不同类型的Cell要使用不同的CellIdentifier。

这个就涉及到UITableView对Cell的重用机制了。关于重用机制,omegayy在IOS Table中Cell的重用reuse机制分析说的很清楚,大家可以去看看。

原创粉丝点击