美文网首页
iOS开发之UI(十一)

iOS开发之UI(十一)

作者: zero点点 | 来源:发表于2016-02-22 22:19 被阅读24次

继续我上一章使用的代码

1.UITableView添加、删除

UITableView编辑步骤

  • 让TableView处于编辑状态
  • 协议设定
    1.确定Cell是否处于编辑状态
    2.设定Cell的编辑样式(删除、添加)
    3.编辑状态进行提交

// 1.让Tableview处于编辑状态
- (void)setEditing:(BOOL)editing {
    // 让父类也进入编辑状态
    [super setEditing:editing];
    // 根据viewcontroller的状态,决定tableView是否进入编辑状态
    [self.tableView setEditing:editing];
}

// 2.确定Cell是否处于编辑状态
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // 默认全部行都可编辑
    return YES;
}

// 3.设定Cell的编辑样式(删除、添加)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;// 返回删除样式
    //return UITableViewCellEditingStyleInsert;// 返回添加样式
}

// 4.编辑状态进行提交
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView beginUpdates];// 表视图开始更新

    // 判断是删除还是添加
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 将该位置下的单元格删除
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        // 如果有数据数组,则删除与该单元绑定的数据
        // [dateArray removeObjectAtIndex:indexPath.row];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // 构建一个位置信息
        NSIndexPath *index = [NSIndexPath indexPathForItem:0 inSection:0];
        // 向tableView中插入单元格
        [tableView insertRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationFade];
        // 向数组中添加数据
        // [dateArray insertObject: atIndex:index.row];
    }

    [tableView endUpdates];
}

2.UITableView移动

  • 实现协议:告诉tableView是否能够移动
  • 移动

// 确定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];

    //删除原位置下的模型数据
    //[_dataArray removeObjectAtIndex:sourceIndexPath.row];

    //在新位置将记录的模型数据添加到数据数组中
    //[_dataArray insertObject:student   atIndex:destinationIndexPath.row];
}

相关文章

  • UI常用的控件

    #iOS开发之UI篇#iOS开发之UI篇 #常用控件介绍1## #UI第09天:滚动视图# ##UIScrollV...

  • iOS开发之UI(十一)

    继续我上一章使用的代码 1.UITableView添加、删除 UITableView编辑步骤 让TableView...

  • KVC

    iOS 如何使用KVC iOS开发UI篇—Kvc简单介绍 iOS开发系列--Objective-C之KVC、KVO

  • IOS学习(9)-UITabBarController

    iOS开发UI篇—UITabBarController简单介绍视图之UITabBarController结构详解(...

  • iOS部分控件介绍及设计规范

    iOS及Android图标按钮设计规范 UI设计师需要了解的开发中常用的UI控件(ios篇) iOS交互设计基础之...

  • 2019-03-22

    iOS 开发之修改图片image颜色 吐槽:平时开发中可能因为 UI妹子懒给到图片中没有需要的颜色,或者嫌弃UI...

  • UITableView ──分页加载

    IOS开发UI展示之UITableView ──分页加载 在ios开中中,由于屏幕尺寸限制,如果需要显示的数据很多...

  • iOS开发之定位

    iOS开发之定位 iOS开发之定位

  • UI第一周学习总结

    ios开发之UI学习第一周总结 UIView基本属性、方法、视图关系、动画 基本属性和方法 UIView:是iOS...

  • iOS开发-UI 从入门到精通(二)

    iOS开发-UI 从入门到精通(二)是对 iOS开发-UI 从入门到精通(一)知识点的巩固,主要以习题练习为主,增...

网友评论

      本文标题:iOS开发之UI(十一)

      本文链接:https://www.haomeiwen.com/subject/mxubkttx.html