UITableView和UITableViewCell的一些简单用法
环境:Xcode4.2,iOS5
1、增加section的标题,
- (
NSString
*)tableView:(
UITableView
*)tableView titleForHeaderInSection:(
NSInteger
)section
{
return @"1";
}
效果如下(“意见反馈”的文字的自己添加的):
2、自定义 section 和 row 的数目(method 的名字很容易看懂,不多解释)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
3、自定义 cell 的样式
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
在该 method 内部,cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;这句是设置箭头的样式。
cell.textLabel.text = @@"CellText";设置 cell 的文字。
4、tableView 的 headerView 和 footerView
在 UITableView 的文档中,tableHeaderView 和 tableFooterView 的说明如下。
tableFooterView
Returns an accessory view that is displayed below the table.
@property(nonatomic, retain) UIView *tableFooterView
tableHeaderView
Returns an accessory view that is displayed above the table.
@property(nonatomic, retain) UIView *tableHeaderView
一般用法如下:
UIView* headerView = …
UITableView* tableView = …
tableView.tableHeaderView = headerView;
这样,我们自定义的一个headerView就会出现在tableView的上方,确切地说,应该是在tableView的table的上面(
above the table)
,即cell的上面。tableFooterView也是同理出现在table的下方。
5、自定义 UITableViewCell
从 UITableViewCell 的官方文档我们可以找到下面一段话
You have two ways of extending the standard
UITableViewCell
object beyond the given styles. To create cells with multiple, variously formatted and sized strings and images for content, you can get the cell's content view (through its
contentView
property) and add subviews to it. You can also subclass
UITableViewCell
to obtain cell characteristics and behavior specific to your application's needs. See "
“A Closer Look at Table-View Cells”
" in
Table View Programming Guide for iOS
即表示有两种方法看自定义cell。一种是使用
contentView
property) and add subviews to it.
一般用法为 [cell.contentView addSubView:subView]; 另一种则是继承了。
在 UITableViewCell 的 detailTextLabel 属性中,可以设置其 Cell Style。而 Cell Style 的其中一个值
UITableViewCellStyleSubtitle
A style for a cell with a left-aligned label across the top and a left-aligned label below it in smaller gray text. The iPod application uses cells in this style.
应该可以用在一个label上下都有文字的情况。需要尝试。
6、UTableView分组(section)显示数据