cell 的选择, 移动 , 删除, 移动, 添加

#import "ViewController.h"


@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>


@end


@implementation ViewController

@synthesize tableView = _tableView;

@synthesize list = _list;


- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    NSMutableArray *array = [[NSMutableArrayalloc] initWithObjects:@"美国",@"菲律宾",

                      @"黄岩岛",@"中国",@"泰国",@"越南",@"老挝",

                      @"日本" ,nil]; 

    self.list = array;


    _tableView=[[UITableViewalloc]initWithFrame:CGRectMake(0,0, self.view.frame.size.width,self.view.frame.size.height)style:UITableViewStylePlain];

    _tableView.delegate=self;

    _tableView.dataSource=self;

    

    

    [self.viewaddSubview:_tableView];


}




#pragma mark - 

#pragma mark Table View Data Source Methods 




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

{

    

  return [self.listcount];

    

    

}


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

{

    

    

    staticNSString *TableSampleIdentifier =@"CellIdentifier";

    

    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:

                             TableSampleIdentifier];

    if (cell ==nil) {

        cell = [[UITableViewCellalloc]

                initWithStyle:UITableViewCellStyleValue1

                reuseIdentifier:TableSampleIdentifier];

    }

    

    NSUInteger row = [indexPathrow];

    cell.textLabel.text = [self.listobjectAtIndex:row];

    UIImage *image = [UIImageimageNamed:@"qq"];

    cell.imageView.image = image;

    UIImage *highLighedImage = [UIImageimageNamed:@"youdao"];

    cell.imageView.highlightedImage = highLighedImage;

    cell.detailTextLabel.text =@"打打打打";

    return cell;

  

}


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


    ///  1.选择      利用cell的accessoryType 判断 是否选择

    UITableViewCell *cell = [tableViewcellForRowAtIndexPath:indexPath];

    if (cell.accessoryType ==UITableViewCellAccessoryNone) {

        cell.accessoryType =UITableViewCellAccessoryCheckmark;

    }else {

        cell.accessoryType =UITableViewCellAccessoryNone;

    }

   // [tableView deselectRowAtIndexPath:indexPath animated:YES];

}




- (void)tableView:(UITableView *)tableView commitEditingStyle:

(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    NSUInteger row = [indexPathrow];


    // 2. 删除

    if (editingStyle ==UITableViewCellEditingStyleDelete) {

        [self.listremoveObjectAtIndex:row]; 

        [tableView deleteRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath]

                         withRowAnimation:UITableViewRowAnimationAutomatic]; 

    }

}


///   3.移动

  [self.tableViewsetEditing:YES];



- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)

sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {

    NSUInteger fromRow = [sourceIndexPathrow]; 

    NSUInteger toRow = [destinationIndexPathrow]; 

    

    id object = [self.listobjectAtIndex:fromRow]; 

    [self.listremoveObjectAtIndex:fromRow]; 

    [self.listinsertObject:object atIndex:toRow]; 

}


//  4.添加

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

{


    [self.listinsertObject:@"你好"atIndex:0];  //   自定义添加数据(会自动刷新表)

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];  /// 可以自定义插入的地方

    [tableView insertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];

   

}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值