(三)UITabBar and UINavigationController基础教程之UITableView的插入删除移动

之前我们实现了页面切换以及UITextField键盘隐藏及防止键盘遮挡


(一)UITabBar and UINavigationController基础教程之切换页面http://blog.csdn.net/zhangyankan/article/details/12833619

(二)UITabBar and UINavigationController基础教程之UITextField键盘隐藏及防止键盘遮挡http://blog.csdn.net/zhangyankan/article/details/12842205


今天我们来实现一下简单的UITableView的插入删除移动


首先我们在.m文件中


#import <UIKit/UIKit.h>
#import "JBaseViewController.h"

@interface JThirdViewController : UITableViewController<UITableViewDataSource,UITableViewDataSource>

@property (strong, nonatomic) NSMutableArray *list;
@end

接下来我们在.h文件中实现


#import "JThirdViewController.h"

@interface JThirdViewController ()

@end

@implementation JThirdViewController
@synthesize list;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[self navigationItem]setRightBarButtonItem:[self editButtonItem]];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"美国", @"菲律宾",@"黄岩岛", @"中国", @"泰国", @"越南", @"老挝",@"日本" , nil];
    self.list = array;
    
    self.navigationItem.title = @"列表";
    
    self.tableView.editing = YES;
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell==nil) {
        cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]autorelease];
//        cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
//        cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];
//        cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier]autorelease];
    }
    
    // Configure the cell...
    NSUInteger row = [indexPath row];
    cell.textLabel.text=[self.list objectAtIndex:row];
    
    UIImage *image = [UIImage imageNamed:@"add_file.png"];
    cell.imageView.image = image;
    UIImage *highLighedImage = [UIImage imageNamed:@"add_list.png"];
    cell.imageView.highlightedImage = highLighedImage;
    
    cell.detailTextLabel.text =@"haha";
    return cell;
}

#pragma mark - Table view delegate

//选中效果
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    NSString *rowString = [self.list objectAtIndex:[indexPath row]];
//    UIAlertView * alter = [[UIAlertView alloc] initWithTitle:@"选中的行信息" message:rowString delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//    [alter show];
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if(cell.accessoryType == UITableViewCellAccessoryNone)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
//        cell.accessoryType = UITableViewCellAccessoryDetailButton;
//        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
//        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

//删除 插入
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = [indexPath row];
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        [self.list removeObjectAtIndex:row];
        [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:
         UITableViewRowAnimationAutomatic
         //UITableViewRowAnimationBottom
         //UITableViewRowAnimationFade
         //UITableViewRowAnimationLeft
         //UITableViewRowAnimationMiddle
         //UITableViewRowAnimationNone
         //UITableViewRowAnimationRight
         //UITableViewRowAnimationTop
         ];
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert)
    {
        NSArray *insertIndexPaths = [NSArray arrayWithObjects:indexPath, nil];
        [self.list insertObject:@"aaaa" atIndex:row];
        [tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationMiddle];
    }
    
}

//移动
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    return UITableViewCellEditingStyleNone;
    return UITableViewCellEditingStyleInsert;
}

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSUInteger fromRow = [sourceIndexPath row];
    NSUInteger toRow = [destinationIndexPath row];
    
    id object = [self.list objectAtIndex:fromRow];
    [self.list removeObjectAtIndex:fromRow];
    [self.list insertObject:object atIndex:toRow];
}


@end

       


具体的介绍可以参考我之前发过的


iOS学习之TableView的简单使用http://blog.csdn.net/zhangyankan/article/details/12776961

iOS学习之分段Table View的使用(Grouped样式表格)http://blog.csdn.net/zhangyankan/article/details/12776965

iOS学习之UITableView中Cell的操作http://blog.csdn.net/zhangyankan/article/details/12776975

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值