tableView编辑
tableView编辑的步骤
1.创建按钮, 用于触发编辑
2.指定哪些可以进行编辑(dataSource方法, 可选)
3.指定编辑样式(删除或添加, delegate的方法, 可选, 默认删除)
4.编辑完成方法中, 进行删除或添加(dataSource的方法, 先删数据, 再删cell)
self.editButtonItem是系统封装好的按钮, 可以做edit和done的切换, 这个按钮关联的方法为setEditing:animated:
self.navigationItem.rightBarButtonItem = self.editButtonItem;
tableview的移动的步骤
1.创建按钮, 用于触发编辑
2.指定那些行可以移动(dataSource方法, 可选)
3.移动完成(dataSource的方法)
4.限制移动(delegate的方法)
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
NSLog(@"%s", __FUNCTION__);
editing, 判断当前是否在编辑
NSLog(@"%d", editing);
让tableView进入编辑状态
UITableView *tableView = (UITableView *)[self.view viewWithTag:1029];
[tableView setEditing:editing animated:YES];
}
- (void)createData {
Animal *animal1 = [Animal animalWithName:@"咪咪" type:@"??"gender:@"母"];
Animal *animal2 = [Animal animalWithName:@"肖恩" type:@"??"gender:@"公"];
Animal *animal3 = [Animal animalWithName:@"舒克" type:@"??"gender:@"公"];
Animal *animal4 = [Animal animalWithName:@"灰太狼" type:@"??"gender:@"公"];
Animal *animal5 = [Animal animalWithName:@"熊大" type:@"??"gender:@"公"];
Animal *animal6 = [Animal animalWithName:@"阿狸" type:@"??"gender:@"母"];
Animal *animal7 = [Animal animalWithName:@"塞文" type:@"??"gender:@"男"];
self.animalArray = [@[animal1, animal2, animal3, animal4, animal5, animal6, animal7] mutableCopy];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _animalArray.count + 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *string = @"OK";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:string] autorelease];
}
if (indexPath.row == self.animalArray.count) {
cell.textLabel.text = @"添加一个动物";
cell.imageView.image = nil;
cell.detailTextLabel.text = @"";
} else {
Animal *animal = _animalArray[indexPath.row];
cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg", animal.name]];
cell.textLabel.text = animal.name;
cell.detailTextLabel.text = animal.type;
}
return cell;
}
控制tableView的哪些可以进行编辑, 默认所有行都可以进行编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// if (indexPath.row == 0) {
// return NO;
// }
return YES;
}
完成编辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
删除操作
1.删除数据源中的数据
[self.animalArray removeObjectAtIndex:indexPath.row];
2.1 重新加载tableView(效率比较低)
// [tableView reloadData];
2.2 删除某行cell
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationAutomatic)];
}
if (editingStyle == UITableViewCellEditingStyleInsert) {
添加操作
1.添加数据
Animal *animal = [Animal animalWithName:@"图图" type:@"??" gender:@"母"];
[self.animalArray insertObject:animal atIndex:2];2.1重新加载tableView
// [tableView reloadData];
2.2 插入cell
定义cell位置
NSIndexPath *index = [NSIndexPath indexPathForRow:2 inSection:0];
插入cell(cell的位置必须和添加数据的位置, 保持一致)
[tableView insertRowsAtIndexPaths:@[index] withRowAnimation:(UITableViewRowAnimationLeft)];
}
}
控制tableView的哪一行可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == self.animalArray.count) {
return NO;
}
return YES;
}
完成移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
// NSLog(@"起点: %ld 终点: %ld", sourceIndexPath.row, destinationIndexPath.row);
数据处理
1.保留原数据
Animal *animal = [self.animalArray[sourceIndexPath.row] retain];
2.删除数据源中的数据
[self.animalArray removeObjectAtIndex:sourceIndexPath.row];
3.插入数据
[self.animalArray insertObject:animal atIndex:destinationIndexPath.row];
4.释放保留的数据
[animal release];
}
#pragma mark - UITableViewDelegate
控制tableView的每一行的编辑样式(insert 或 delete)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == self.animalArray.count) {
return UITableViewCellEditingStyleInsert;
}
return UITableViewCellEditingStyleDelete;
}
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
判断是否是第一行或者是最后一行
if (proposedDestinationIndexPath.row == 0 || proposedDestinationIndexPath.row == self.animalArray.count) {
//如果是, 返回原来位置
return sourceIndexPath;
} else {
如果不是, 返回终点位置
return proposedDestinationIndexPath;
}
}
Animal.m
#import "Animal.h"
@implementation Animal
- (void)dealloc {
[_name release];
[_type release];
[_gender release];
[super dealloc];
}
- (instancetype)initWithName:(NSString *)name type:(NSString *)type gender:(NSString *)gender {
if (self = [super init]) {
self.name = name;
self.type = type;
self.gender = gender;
}
return self;
}
+ (instancetype)animalWithName:(NSString *)name type:(NSString *)type gender:(NSString *)gender {
return [[[Animal alloc] initWithName:name type:type gender:gender] autorelease];
}
- (NSString *)description
{
return [NSString stringWithFormat:@"name: %@ type: %@ gender: %@", _name, _type, _gender];
}
@end
UITableViewController, 继承于UIViewControl, 自带一个UITableView
UITableViewController的优势
1.不需要再创建UITableView
2.不需要指定delegate和dataSource, 也不需要遵守<UITableViewDelegate,UITableViewDataSource>
#import "RootTableViewController.h"
@interface RootTableViewController ()
@end
@implementation RootTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.rowHeight = 100;
self.navigationItem.rightBarButtonItem = self.editButtonItem;
//是否支持多选, 默认为NO
self.tableView.allowsMultipleSelection = YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UITableviewdatasource
//分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier] autorelease];
}
cell.textLabel.text = @"Hello";
return cell;
}
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
#pragma mark - UITableViewDelegate
//行高, cell高度不一致
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row % 2 == 1) {
return 50;
} else {
return 100;
}
}
//区头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 60;
}
//区尾高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0;
}
//自定义区头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tu.jpg"]];
// imageView.contentMode = UIViewContentModeScaleAspectFit;
return imageView;
}
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row % 2 == 1) {
return UITableViewCellAccessoryDetailButton;
} else {
return UITableViewCellAccessoryCheckmark;
}
}
//点击了哪个附加视图的代码
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%ld, %ld", indexPath.section, indexPath.row);
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"选中%ld行", indexPath.row);
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"取消了%ld行", indexPath.row);
}
//设置删除按钮名称
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return @"OK";
}
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleDefault) title:@"收藏" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
NSLog(@"已经收藏");
}];
UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleNormal) title:@"分享" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
NSLog(@"已经分享");
}];
return @[action1, action2];
}
@end
原文:http://www.cnblogs.com/OrangesChen/p/4926111.html