iOS table简单实现增加移动和删除功能与自定义cell的实现

RootViewController.h文件中

#import <UIKit/UIKit.h>


@interface RootViewController :UIViewController

{

   BOOL editState;

}


//@property (nonatomic,retain)NSArray *noteList;

@property (nonatomic,retain)NSMutableArray *noteList;

@property (weak, nonatomic) IBOutletUITableView *mTableView;


//添加移动操作

- (void)noteMove;


//添加删除

- (void)noteDelete:(id)sender;

@end


RootViewController.m

#import "RootViewController.h"


@interface RootViewController () <UITableViewDataSource,UITableViewDelegate>


@end


@implementation RootViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

   self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];

   if (self) {

       self.title =@"简单的表";

    }

    return self;

}


- (void)viewDidLoad

{

    [superviewDidLoad];

    NSMutableArray *array = [[NSMutableArrayalloc]initWithObjects:@"2009-12-1",@"2009-12-2",@"2009-12-3",@"2009-12-4",@"2009-12-5",@"2009-12-6",nil];

   self.noteList = array;

    

    UIBarButtonItem *moveButton = [[UIBarButtonItemalloc]initWithTitle:@"Move"style:UIBarButtonItemStyleBorderedtarget:selfaction:@selector(noteMove)];

    self.navigationItem.rightBarButtonItem = moveButton;

    

    UIBarButtonItem *deleteButton = [[UIBarButtonItemalloc]initWithTitle:@"delete"style:UIBarButtonItemStyleBorderedtarget:selfaction:@selector(noteDelete:)];

    self.navigationItem.leftBarButtonItem = deleteButton;

}

//添加行的移动

- (void)noteMove

{

    editState = YES;

    [self.mTableViewsetEditing:!self.mTableView.editinganimated:YES];

}

//添加行的删除

- (void)noteDelete:(id)sender

{

    editState =NO;

    [self.mTableViewsetEditing:!self.mTableView.editinganimated:YES];

}

//返回指定分期的行数分区默认是1

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

   return [self.noteListcount];

}

//

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

   static NSString *NoteScanIdentifier =@"NoteScanIdentifier";

   UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:NoteScanIdentifier];

    //这里使用NoteScanIdentifer类型的可重用单元检查一下单元是否为空(nil),如果是,就要使用前面所提到的标识符字符串来创建一个新的表视图单元。

   if(cell == nil)

    {

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:NoteScanIdentifier];

        cell.showsReorderControl =YES;//为行指定了标准扩展图标,只有表处于编辑时才可以显示,通过设置表视图单元的这个属性,才能启动移动控件,可以进入移动的状态。

    }

   NSUInteger row = [indexPath row];

    cell.textLabel.text = [_noteListobjectAtIndex:row];

   UIImage *image = [UIImageimageNamed:@"aiside.png"];

    cell.imageView.image = image;

   return cell;

}

/*行的移动方法*/


//确认编辑类型

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

    if(!editState)//是否处于编辑状态

        returnUITableViewCellEditingStyleDelete;

   else

        returnUITableViewCellEditingStyleNone;

}

//指定该行能够移动

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{

    if(editState)

       return YES;

   else

        returnNO//如果点了删除行不能移动

}

//移动方法

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

{

   NSUInteger fromRow = [sourceIndexPath row];    //要移动的位置

   NSUInteger toRow = [destinationIndexPath row]; //移动的目的位置

   id object = [_noteListobjectAtIndex:fromRow]; //存储将要被移动的位置的对象

    [_noteListremoveObjectAtIndex:fromRow];       //将对象从原位置移除

    [_noteListinsertObject:object atIndex:toRow]; //将对象插入到新位置

}

//删除cell方法

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

   NSUInteger row = [indexPath row]; //获取当前行

    [self.noteListremoveObjectAtIndex:row]; //在数据中删除当前对象

    [_mTableViewdeleteRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];//数组执行删除操作

}


@end


运行结果





自定义cell

xib文件


CustomCell.h文件为:

#import <UIKit/UIKit.h>


@interface CustomCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *dateLabel;

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;


@end


RootViewController.m改为


//

//  RootViewController.m

//  实现一个简单的表

//

//  Created by qin on 14-3-30.

//  Copyright (c) 2014 zhou. All rights reserved.

//


#import "RootViewController.h"

#import "NoteDetailController.h"

#import "CustomCell.h"

#import "pxAppDelegate.h"

@interface RootViewController () <UITableViewDataSource,UITableViewDelegate>


@end


@implementation RootViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        self.title = @"简单的表";

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    self.title = @"Note Scan";

    NSDictionary *row1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"2009-12-01",@"Date",@"学习 object-c",@"Title", nil];

    NSDictionary *row2 = [[NSDictionary alloc]initWithObjectsAndKeys:@"2009-12-02",@"Date",@"学习 Cocoa",@"Title", nil];

    NSDictionary *row3 = [[NSDictionary alloc]initWithObjectsAndKeys:@"2009-12-03",@"Date",@"学习 iphone开发",@"Title", nil];

    NSDictionary *row4 = [[NSDictionary alloc]initWithObjectsAndKeys:@"2009-12-04",@"Date",@"项目需求分析",@"Title", nil];

    NSDictionary *row5 = [[NSDictionary alloc]initWithObjectsAndKeys:@"2009-12-05",@"Date",@"项目讨论",@"Title", nil];

    NSDictionary *row6 = [[NSDictionary alloc]initWithObjectsAndKeys:@"2009-12-06",@"Date",@"项目开发",@"Title", nil];

    NSArray *array = [[NSArray alloc]initWithObjects:row1,row2,row3,row4,row5,row6 ,nil];

    

    self.noteList = array;

}

//添加行的移动

- (void)noteMove

{

    editState = YES;

    [self.mTableView setEditing:!self.mTableView.editing animated:YES];

}

//添加行的删除

- (void)noteDelete:(id)sender

{

    editState =NO;

    [self.mTableView setEditing:!self.mTableView.editing animated:YES];

}

//返回指定分期的行数 分区默认是1

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return [self.noteList count];

}

//

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *CustomCellIdentifier = @"CustomCellIdentifier";

    //自定义cell

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];

    

    //这里使用NoteScanIdentifer类型的可重用单元 检查一下单元是否为空(nil),如果是,就要使用前面所提到的标识符字符串来创建一个新的表视图单元。

    if(cell == nil)

    {

        NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"CustomCell" owner:self options:nil];

        cell = [nib objectAtIndex:0];  //xib中的第一个对象赋给cell

    }

    NSUInteger row = [indexPath row];

    NSDictionary *rowdata = [self.noteList objectAtIndex:row];

    cell.dateLabel.text = [rowdata objectForKey:@"Date"];

    cell.titleLabel.text = [rowdata objectForKey:@"Title"];

    [cell setAccessoryType:UITableViewCellAccessoryDetailButton];

    

    return cell;

}

/*行的移动方法*/


//确认编辑类型

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

    if(!editState) //是否处于编辑状态

        return UITableViewCellEditingStyleDelete;

    else

        return UITableViewCellEditingStyleNone;

}

//指定该行能够移动

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{

    if(editState)

        return YES;

    else

        return NO //如果点了删除行不能移动

}

//移动方法

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

{

    NSUInteger fromRow = [sourceIndexPath row];    //要移动的位置

    NSUInteger toRow = [destinationIndexPath row]; //移动的目的位置

    id object = [_noteList objectAtIndex:fromRow]; //存储将要被移动的位置的对象

    [_noteList removeObjectAtIndex:fromRow];       //将对象从原位置移除

    [_noteList insertObject:object atIndex:toRow]; //将对象插入到新位置

}

//删除cell方法

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSUInteger row = [indexPath row]; //获取当前行

    [self.noteList removeObjectAtIndex:row]; //在数据中删除当前对象

    [_mTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; //数组执行删除 操作

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"查看详细信息" message:@"如果要查看详细信息请点击蓝色按钮" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];

    [alert show];

}

//点击公开按钮后触发的操作

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

{

    NSLog(@"点击了蓝色按钮!!");

    if(subController == nil)

    {

        subController = [[NoteDetailController alloc]initWithNibName:@"NoteDetailController" bundle:nil];

    }

    subController.title = @"详细信息";

    NSUInteger row = [indexPath row];

    

    NSDictionary *seletedDate = [_noteList objectAtIndex:row];

    NSString *selectStr = [seletedDate objectForKey:@"Date"];

    NSString *titleStr = [seletedDate objectForKey:@"Title"];

    NSString *detailMessage = [[NSString alloc]initWithFormat:@"详细内容:%@---->%@",selectStr,titleStr];

    subController.message = detailMessage;

    

//    UINavigationController *nav = [[UINavigationController alloc]init];

//    [nav pushViewController:subController animated:YES];

    pxAppDelegate *delegate = [[UIApplication sharedApplication]delegate];

    [delegate.nav pushViewController:subController animated:YES];

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 60; //cell的高度

    

}

@end


运行结果:

          

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值