UITableView的简单实用

#import "RootTableViewController.h"


@interface RootTableViewController ()


@property(nonatomic, retain)NSMutableArray *darasource;



@end


@implementation RootTableViewController


//创建一个空的数组

- (NSMutableArray *)darasource{


    if (!_darasource) {

        self.darasource = [NSMutableArray array];

    }

    return _darasource;

}



- (void)viewDidLoad {

    [super viewDidLoad];

    

      //为对应的重用标识符指定单元格类型

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];

    

    // Uncomment the following line to preserve selection between presentations.

    // self.clearsSelectionOnViewWillAppear = NO;

    

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.

    //如果表视图需要通过编辑按钮来切换其编辑状态,则只需要调用editButtonItem方法来获取一个编辑按钮,添加在导航条上

     self.navigationItem.rightBarButtonItem = self.editButtonItem;

    

    //数组加入数据

    [self.darasource addObject:@"第一行"];

    [self.darasource addObject:@"第二行"];

    [self.darasource addObject:@"第三行"];

    [self.darasource addObject:@"第四行"];

    [self.darasource addObject:@"添加最新一行"];

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


#pragma mark - Table view data source


- (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 self.darasource.count;

}



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

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];

    

    // Configure the cell...

    cell.textLabel.text = self.darasource[indexPath.row];

    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.

    //该协议方法是询问代理对象对应行是否允许被进行编辑操作。(通过重写改协议方法可以让表视图支持条件编辑,默认为全部允许编辑)

//    if (indexPath.row ==0) {

//        return NO;

//    }

    return YES;

}



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


    //为表视图的对应行指定编辑样式,样式有:插入,删除,选中和无

    if (indexPath.row == self.darasource.count-1) {

        return UITableViewCellEditingStyleInsert;//添加样式

    }

    

    return UITableViewCellEditingStyleDelete ;//删除样式

//    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;//选择样式


}








// 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

        

        [self.darasource removeObjectAtIndex:indexPath.row];//删除数据源

        [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

        [self.darasource insertObject:@"新插入的数据" atIndex:indexPath.row];

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

        

    }

    

}



//移动单元格的3个步骤

// Override to support rearranging the table view.

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {

    //在界面移动完成时,通过此协议方法来更新数据源中的元素顺序

    //1.先获取被移动的数据

    id object = [self.darasource [fromIndexPath.row] retain];

    //2.将该对象从数组中移除

    [self.darasource removeObjectAtIndex:fromIndexPath.row];

    //3.将该对象插入到指定下标

    [self.darasource insertObject:object atIndex:toIndexPath.row];

    //4.释放对象的所有权

    [object release];

    

}




// 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.

    if (indexPath.row ==0) {

        return NO;

    }

    

    return YES;

}




/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值