UITableView的详细使用

// 在RootViewController.h文件中,需要遵守UITableViewDelegate和UITableViewDataSource协议,还声明了两个NSMutableArray数组用来存放各个分组中的单元格。一个UITableView用来显示一个UITableViewCell对象,代码如下:
#import
@interface RootViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) UITableView *myTableView;
@property (nonatomic, retain) UITableViewCell *tableViewCell;
@property (nonatomic, retain) NSMutableArray *tableViewDataSource; // TableView中显示的数据
@property (nonatomic, retain) NSMutableArray *tableViewDateSource2; // 第二个分组使用的数据
@end
// 主要的方法有如下,


#pragma mark 设置TableView上面显示的内容 使用此方法时候需要注意,一定要使用重用机制,否则会出现文字或图像重叠的现象
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;


#pragma mark - 重写----设置有多少个分组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;


#pragma mark - 重写----设置每个分组上面有多少单元格
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;


#pragma mark - 重写---修改行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 44; // 返回多少即设置行高为多少

}

#pragma mark - 重写----设置单元格分组的标题和标注的高度
#pragma mark 设置分组上面的标题的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
#pragma mark 设置分组下面的标注的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;



#pragma mark - 重写----设置单元格分组的标题
// 可以设置为UIView类型
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
// 也可以直接设置为字符串

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;


#pragma mark - 重写----设置分组下面显示的视图
// 设置为UIView类型
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
// 也可以直接设置为字符串类型

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;



#pragma mark - 重写----设置当我们点击Edit按钮,让TableView进入编辑状态时候左面显示出得按钮是加号、减号、还是空白
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
// 主要由如下三种情况

// UITableViewCellEditingStyleNone // 显示为空白
// UITableViewCellEditingStyleDelete // 显示为减号
// UITableViewCellEditingStyleInsert // 显示为加号
#pragma mark - 重写----当TableView将要显示的时候进行设置 可以设置TableView隔行换背景色
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;



#pragma mark - 重写----设置可以编辑 返回值为YES  
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
#pragma mark - 重写----设置可以排序移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
#pragma mark - 重写----排序移动时的方法
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;



#pragma mark - 重写----判断是那个编辑的状态,然后依据状态去编辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;



#pragma mark - 重写---点击了每个单元格(Cell)触发的事件,如果可以移动的话就必须使用标题来判断
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;






// 以上为经常使用的几个方法,下面贴上添加内容,移动,添加,删除的主要代码:
// 添加内容的主要代码:

#pragma mark 设置TableView上面显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 首先先声明一个标识符
NSString *cell_id = @"cell_id";
// 从TableView的重用队列中获取可重用的单元格
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id];
// 刚开始因为重用队列中没有可重用的单元格,所有cell不存在,需要主动实例化指定样式
if (!cell) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cell_id] autorelease];
    // 设置每一个单元格右面显示的符号
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
// 实例化可变数据,作为数据源
NSUInteger row = [indexPath row];
// 设置不同分组的,如果是普通列表形式,则无需if判断
if (indexPath.section == 0) {
    // 设置单元格上面的内容
    cell.textLabel.text = [self.tableViewDataSource objectAtIndex:row];
} else if (indexPath.section == 1) {
    // 设置单元格上面的内容
    cell.textLabel.text = [self.tableViewDateSource2 objectAtIndex:row];
}
return cell;
}

// 移动排序的主要代码:

#pragma mark - 重写----设置可以编辑 返回值为YES  
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

#pragma mark - 重写----设置可以排序移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;

}

#pragma mark - 重写----排序移动时的方法
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
// 获取需要移动的行
NSInteger fromRow = sourceIndexPath.row;
// 获取移动某处的位置
NSInteger toRow = destinationIndexPath.row;
// 从数组合中读取需要移动行的数据
id object = [_tableViewDataSource objectAtIndex:fromRow];
// 把数组中移除需要移动的行的数据
[_tableViewDataSource removeObjectAtIndex:fromRow];
// 把需要移动的数据插入到目标位置
[_tableViewDataSource insertObject:object atIndex:toRow];   
}


// 添加新的单元格(cell)的主要代码
// 首先需要在头文件中声明一个cellCount和UITableViewCell以供使用

#import
@interface InsertTableViewController : UITableViewController
@property (nonatomic, assign) NSUInteger cellCount;
@property (nonatomic, retain) UITableViewCell *nullTableViewCell;
@property (nonatomic, retain) NSMutableArray *tableViewDataSource;
@end
// 实现文件中进行主要操作

#pragma mark - 重写----设置编辑状态下显示的按钮是加号、减号、空
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleInsert;
}


#pragma mark - 重写----依据编辑状态进行相应的操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleInsert) { // 如果为添加按钮状态
    // 每增加一次,数量就会加1
    _cellCount ++;
    // 获取增加的位置
    NSUInteger row = indexPath.row;
    // 初始化一个数组,装载indexPath
    NSArray *insertIndexPath = [NSArray arrayWithObjects:indexPath  , nil];
    // 设置新添加的内容的标题
    NSString *message = [NSString stringWithFormat:@"new TableViewCell %d", _cellCount];
    // 添加单元格,设置标题
    [self.tableViewDataSource insertObject:message atIndex:row];
    [tableView insertRowsAtIndexPaths:insertIndexPath withRowAnimation:UITableViewRowAnimationLeft];
}   
}


// 删除单元格的主要代码:

#pragma mark - 重写----设置编辑状态下显示的按钮的样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

#pragma mark - 重写----判断是哪个编辑状态,进行相应的操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) { // 删除单元格
    // 通过索引值,删除数组中的数据
    [_tableViewDataSource removeObjectAtIndex:indexPath.row];
    // 删除单元格中的某一行,使用动画效果实现
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
}   
}


// 还有就是UINavigationController右上角的Edit按钮,如果你写的类继承自UITableViewController类,则直接在viewDidLoad方法中写上 self.navigationItem.rightBarButtonItem = self.editButtonItem; 就可以了。如果你写的类不是继承UITableViewController,则需要你手动写一个UIBarButtonItem了,下面为主要代码:

// 在NavigationController上面显示出编辑按钮
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStyleBordered  target:self action:@selector(toggleEdit:)];
self.navigationItem.rightBarButtonItem = editButton;


#pragma mark - 设置NavigationBar右上角的按钮的不同状态显示的不同的文字和是否处于编辑的状态
- (void)toggleEdit:(UIBarButtonItem *)sender
{
if (self.myTableView.isEditing == YES) {
    [self.myTableView setEditing:NO animated:YES];
    [sender setTitle:@"编辑"];
} else {
    [self.myTableView setEditing:YES animated:YES];
    [sender setTitle:@"完成"];
}
}



// 我们在初始化UITableView的时候,一定要把delegate和dataSource代理设置为本身,否则是不起作用的

_myTableView.dataSource = self; // 设置数据代理为本身
_myTableView.delegate = self; // 设置样式代理为本身