tabview列表样式:
UITableViewStylePlain--不分组
UITableViewStyleGrouped--分组
numberOfSectionlInTableView--Section的数量
每个section的页眉和页尾--数据源的方法:tableView:titleForHeaderInSection, tableView:titleFooterInSection
返回字符串,默认是nil
页眉和页尾的视图:
tableView:viewForHeaderInSection,
tableView:viewFooterInSection
页眉和页尾的高度是默认由自表自身决定的,该表有两个属性:sectionHeaderHeght,sectionFooterHeight;可以通过委托方法来覆盖:tableView:heightForHeaderInSection,tableVive:heightForFooterInSection;
一般在UITableViewController的子类上进行UItableViewDelegate上的操作和UITableViewDataSource上的数据操作;
必须要实现的方法:
1.每个section有固定的行数的话就用:- (NSInteger)tableView:(UITableView *)tableView numberOfRpwsInSection:(NSInteger)section;
2.必须要实现的,返回某一行的cell数据-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
NSIndexPath可以确定表单元的位置(section和row)
表视图的section返回0时表示没有数据,会阻止表视图的其他请求;
cell重用会调用次方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@”MyIdentifier”];
if(cell == nil)
{
cell = [[UITabelView alloc]initWithStyle:... reuseIdentifier:@””
}
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear;animated];
[self.tableView reloadData];
}
视频笔记:
无格式表视图和分段表视图(普通,分组,索引)
每一行叫做单元格,可以放置控件.
封装成属性.
选中单元格后触发的方法:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//
// RootViewController.m
// 20-02
//
// Created by IT小怪才 on 12-11-2.
// Copyright (c) 2012年 IT小怪才. All rights reserved.
//
#import "RootViewController.h"
#import "item.h"
@interface RootViewController ()
@end
@implementation RootViewController
@synthesize tableView;
@synthesize items;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
items = [[NSMutableArray alloc] initWithCapacity:20];
tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStyleGrouped];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
item *msg;
msg = [[item alloc] init];
msg.text = @"小葱拌豆腐";
msg.checked = NO;
[items addObject:msg];
msg = [[item alloc] init];
msg.text = @"木桶饭";
msg.checked = YES;
[items addObject:msg];
msg = [[item alloc] init];
msg.text = @"姜汁松花蛋";
msg.checked = YES;
[items addObject:msg];
msg = [[item alloc] init];
msg.text = @"麻辣香锅";
msg.checked = NO;
[items addObject:msg];
msg = [[item alloc] init];
msg.text = @"练字!!!";
msg.checked = NO;
[items addObject:msg];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(Clicked:)];
self.navigationItem.rightBarButtonItem = button;
}
- (void) Clicked:(id)sender {
int newRowIndex = [items count];
item *msg = [[item alloc] init];
msg.text = @"老大,我是新来的!";
msg.checked = NO;
[items addObject:msg];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:newRowIndex inSection:0];
//此方法需要行切入以及Section.
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
//打包成数组
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
//添加到界面上
}
- (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
[items removeObjectAtIndex:indexPath.row];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
//此方法需要传入删除的cell的坐标;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [items count];
}
- (float) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 50.0f;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellName = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName] autorelease];
}
item *msg = [items objectAtIndex:indexPath.row];
[self configureTextForCell:cell withChecklistItem:msg];
[self configureCheckmarkForCell:cell withChecklistItem:msg];
return cell;
}
//每行的数据赋值
- (void)configureTextForCell:(UITableViewCell *)cell withChecklistItem:(item *)item {
cell.textLabel.text = item.text;
}
- (void)configureCheckmarkForCell:(UITableViewCell *)cell withChecklistItem:(item *)item {
if (item.checked) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
//选中函数
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
item *msg = [items objectAtIndex:indexPath.row];
[msg toggleChecked];//取反函数
[self configureCheckmarkForCell:cell withChecklistItem:msg];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end