cell 内容编辑

来源:互联网 发布:岑村转让网络 编辑:程序博客网 时间:2024/05/18 00:56

MainViewController.m

#import "MainViewController.h"@interface MainViewController ()<UITableViewDelegate,UITableViewDataSource>@property(nonatomic,retain)UITableView *tableView;@property(nonatomic,retain)NSMutableArray *arr;@end@implementation MainViewController-(void)dealloc{    [_arr release];    [_tableView release];    [super dealloc];}-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花容",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松",nil];    }    return self;}- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    self.navigationController.navigationBar.translucent=NO;    self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64) style:UITableViewStyleGrouped];    [self.view addSubview:self.tableView];    [self.tableView release];    self.tableView.dataSource=self;    self.tableView.delegate=self;    // 系统提供的属性    self.navigationItem.rightBarButtonItem=self.editButtonItem;}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return self.arr.count;}-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return 1;}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *reuse=@"reuse";    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reuse];    if (!cell) {        cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse]autorelease];    }    cell.textLabel.text=self.arr[indexPath.row];    return cell;}#pragma mark 重写系统的编辑按钮点击触发方法 在这里并没有实现方法 实现方法在后面的commit方法中-(void)setEditing:(BOOL)editing animated:(BOOL)animated{    [super setEditing:editing animated:animated];    [self.tableView setEditing:editing animated:YES];}#pragma mark 设置哪些行可以进行编辑-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{    return YES;}//-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath //{//    if (indexPath.row==1) {//        return YES;//    }//    return NO;//}// 将cell移动位置  换行-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{     // 3步    // 1.先获取到起始位置的数据    NSString *str=[self.arr[sourceIndexPath.row]retain];    // 2.将起始位置的对象从数据源中移除    [self.arr removeObjectAtIndex:sourceIndexPath.row];    // 3.把数据插入到数组的目的地位置上去    [self.arr insertObject:str atIndex:destinationIndexPath.row];    /*     如果不写第二步的话,数据源中的数据并没有移除,只不过是视觉上数据变换了位置,其实并没有,用NSLOG打印的话即使将cell移动了,打印结果还是没有变化,是原始self.arr 中的数据     但是如果写了第二步的话,数据源中的数据也发生了变化,根据移动cell的不同,打印结果也不同     */    NSLog(@"%@",self.arr[1]);    NSLog(@"%@",self.arr[2]);    [str release];}// 如果想要修改Footer 和Header 的高度的话可以用-(CGFloat)tableView:(UITableView *) heightForFooterInSection:(NSInteger)section    和Header......;-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{    return @"limuran";}// Footer如果不写titleForFooterInSection的方法也可以直接实现,尺寸是默认的,修改不了,但是如果写了titleForFooterInSection方法的话,尺寸默认高为30;,如果不写titleForFooterInSection方法的话同样可以在View上面铺控件. 铺的控件尺寸可以超过View-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{    UIView *footerView=[[UIView alloc]initWithFrame:self.view.frame];    footerView.backgroundColor=[UIColor yellowColor];    [self.view addSubview:footerView];    [footerView release];    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150, 40)];    label.text=@"jfdklsa";    label.backgroundColor=[UIColor redColor];    [footerView addSubview:label];    [label release];    return footerView;}-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    return @"fdjskajlds";}// Header必须写TitleForHeaderInSection方法才能实现// 如果Header不写title方法的话View不会显示..如果想要在Header的位置上铺控件的话一定要写title方法,然后在View上铺.-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{    UIView *headerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 30)];    headerView.backgroundColor=[UIColor redColor];    [self.view addSubview:headerView];    [headerView release];    return headerView;}#pragma mark 这个方法是iOS8.0之后出现的方法,可以在编辑状态的时候有多个按钮-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewRowAction *deleteAction=[UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {        // 按钮的点击所要触发的事件,都是写在block中        NSLog(@"触发了删除按钮");    }];    deleteAction.backgroundColor=[UIColor yellowColor];    UITableViewRowAction *AddAction=[UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"添加" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {        NSLog(@"触发了添加按钮");    }];    return @[deleteAction,AddAction];}// 修改删除按钮的标题-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{    return @"点点";}-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{    // cell编编辑辑的样式    if (indexPath.row%2==0) {        return UITableViewCellEditingStyleDelete;    }    return UITableViewCellEditingStyleInsert;}// 编辑数据// 该方法提供了左滑编辑的功能(是一个隐藏功能)-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{//    if (editingStyle==UITableViewCellEditingStyleDelete) {//        [self.arr removeObjectAtIndex:indexPath.row];////        [self.tableView reloadData];//        //        // 第一个参数:指定删除哪一个分区的哪个行,把他作为一个元素放在数组中//        // 第二个参数:删除动画//        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];//    }    if (editingStyle==UITableViewCellEditingStyleInsert) {        [self.arr insertObject:@"limuran" atIndex:indexPath.row];        [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];    }}
0 0
原创粉丝点击