UITableView

1 创建UITableView

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

可以设置分割线样式 和颜色

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

2 UITableView有两个协议,分别是

UITableViewDataSource, UITableViewDelegate

3 协议内必须实现的两个方法分别是

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;设置cell

4 可以设置cell的高 、cell的点击方法、section分区的个数 以及分区的标题等

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

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;

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

另外关于cell的编辑 删除 移动 等操作的方法

1 删除分为四步

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

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

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

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

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

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

1.4 指定的编辑状态下的方法

- (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];//表视图结束更新

}

2.1 移动

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

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

2.2  指定那些可以移动的

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

    return YES;

}//默认都可以移动 

2.3  移动完成之后要做的事情,怎样完成移动

- (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列表

}

关于协议传值的使用;分为5步

声明协议的一方 内有三步:

1 签订协议

2 声明代理 assign

3 判断协议是否执行,协议代理人是否指定 ,执行协议的方法

接受协议的一方两步

4 指定代理人

5 执行协议的方法 

关于解析数据模型的创建 

1 copy来修饰

2 声明的名字一定要一致 否则解析数据会出错

3 异常处理防止崩溃的方法要重写

内部可以写关于id与系统内部关键字重名 等的处理 

原文地址:https://www.cnblogs.com/jiurong001/p/5183750.html