iOS基础之UITableView

  UITableView继承于UIScrollView,可以滚动。

   UITableView的每一条数据对应的单元格叫做Cell,是UITableViewCell 的一个对象,继承于UIView。

   UITableView可以分区显示, 每一个分区称为secation, 每一行称为 row, 编号都从0开始。

   系统提供了一个专门的类来整合section和row,叫做NSIndexPath。

  代码演示:

  

// 创建UITableView

_tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];

//设置分割线样式 和颜色

_tableView.separatorColor = [UIColor orangeColor];  _tableView.separatorStyle = UITableViewCellSelectionStyleBlue;

//声明 UITableView的两个协议:UITableViewDataSource,UITableViewDelegate

//实现协议内必须的方法分
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;//设置cell

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;//设置cell的个数

//设置cell的高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
//section分区的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
//section分区的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
//选中哪一个cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;


//对cell的一些操作

// 先让cell处于编辑状态打开

- (void)setEditing:(BOOL)editing animated:(BOOL)animated;

// 指定表视图 中那个cell可以编辑

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;

// 指定编辑的样式是删除还是 添加

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

// 指定的编辑状态下的方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{    

    [tableView beginUpdates];//表视图开始更新

        //判断编辑样式

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        //1 将该位置下的单元格删除

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

        //2 删除数据数组中,与该单元格绑定的数据

        [_dataArray removeObjectAtIndex:indexPath.row];

    }else if(editingStyle == UITableViewCellEditingStyleInsert) {

        //找到点击 点

        Student *student = _dataArray[indexPath.row];

        //构建一个位置信息

        NSIndexPath *index = [NSIndexPath indexPathForItem:0 inSection:0];//插入到顶部

        [tableView insertRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationTop];//插入单元格

        [_dataArray insertObject:student atIndex:index.row];

    }

    [tableView endUpdates];//表视图结束更新

}


//移动cell
// 先让cell处于编辑状态打开

- (void)setEditing:(BOOL)editing animated:(BOOL)animated;

//  指定可以移动的cell

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

    return YES;

}//默认都可以移动 

//怎样完成移动

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

    //先记录原有位置下的模型数据

    Student *student = _dataArray[sourceIndexPath.row];

    [student retain];// 加一 防止野指针

    if (destinationIndexPath.section == sourceIndexPath.section) { //条件判断在本分区内部 进行移动        

    //删除原有位置下的模型数据

    [_dataArray removeObjectAtIndex:sourceIndexPath.row];

    //在新位置将记录的模型数据添加到数据数组中去

    [_dataArray insertObject:student atIndex:destinationIndexPath.row];

    }

    [student release];    

    [self.tableView reloadData];//编辑完成之后。刷新tableView列表

}
原文地址:https://www.cnblogs.com/16-jkd/p/5205926.html