• 07-UIKit(tableview的编辑模式、accessoryView)


    目录:

    一、tableview的编辑模式-增删改查

    二、不使用继承创建tableview

    三、accessoryView辅助视图

    回到顶部

    一、tableview的编辑模式-增删改查

    [1-contact-edit]

    增:

    1对数据模型增加数据

          self.contacts addObject:

    2对tableview增加行

          self.tableView insertRowsAtIndexPaths

    删改查:

    tableview进入编辑模式

    1如何设置编辑模式UITableView.editing

    2能干什么:添加、删除、移动

    这些都是对cell进行的操作

    3进入编辑模式的快捷方式是把viewDidLoad中的self.editButtonItem显示出来,一旦按了按钮,tableview就会进入编辑模式,默认情况下是删除模式

    4如果需要达到删除的效果需要进行两问一响应,两个问题可以不回答,但必须有响应

    两问:

    1)第一问哪些行可以进行编辑,哪些不能

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

    2)第二问哪些行可以进行删除,哪些行可以进行插入

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

    3)一响应当用户选择了行进行删除或插入,如何处理

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

    回到顶部

    二、不使用继承创建tableview

    [2-tableView]

    并提供三问一响应的链接dataSource和delegate到要提供数据的viewcontroller,这个viewcontroller要遵守detaSource这个协议

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        UITableView *tableView = [[UITableView alloc] init];
        tableView.frame = CGRectMake(160, 42, 122, 322);
        tableView.dataSource = self;
        [self.view addSubview:tableView];
        // Do any additional setup after loading the view, typically from a nib.
    }

    静态的tableview

    分区和行数都是固定的,如果用代码实现都是直接写死的。

    回到顶部

    三、accessoryView辅助视图

    是tableview中cell的子试图中的一个,

    有哪些类型:iOS6之前有4种iOS7增加了一种,

    干什么用呢?触发高级事件

    //返回cell数据
    - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *cellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        cell.textLabel.text = @"ddd";
        //设置辅助视图  也就是cell右边的那个视图
        
        if (indexPath.row == 0) {
            //开关
            cell.accessoryView = [[UISwitch alloc] init];
        }else{
            cell.accessoryType = UITableViewCellAccessoryDetailButton;
        }
        return cell;
    }
    //点击cell之后调用的方法
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        NSLog(@"+++");
    }
    //点击辅助视图调用的方法
    - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
        NSLog(@"---");
    }

    作业:

          1. 在一个界面上有两个TableView, 其中一个显示城市列表,如:北京,上海, 广州..,另一个显示用户选中的那个城市的行政区列表,如: 东城,西城,…, 当用户选中了某个行政区,界面上的一个TextView就显示此行政区的简介。

          数据模型自己设计。

     

          2. TMusic的设置界面,根据给的资源包高仿QQ音乐的设置界面。

    补充:

    UI控件用weak,NSString用copy,其他对象一般用strong

    tableviewcell的高度有一个代理方法来调

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

    字符串追加

    cell.detailTextLabel.text = [[contact.phoneName stringByAppendingString:@" "] stringByAppendingString:contact.iphone];

    //更新界面

        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.contacts.count -1 inSection:0];

       

        [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

  • 相关阅读:
    Express本地测试HTTPS
    在 WebStorm 中,配置能够识别 Vue CLI 3 创建的项目的别名 alias
    在线版本的ps
    功能强大的任务日历组件
    tree-shaking实战
    深入diff 算法
    【题解】[SHOI2001] Panda 的烦恼
    【题解】[JLOI2011]不重复数字
    「Codeforces Global Round #10」赛后个人总结
    【题解】[SCOI2004] 文本的输入
  • 原文地址:https://www.cnblogs.com/yangmx/p/3516548.html
Copyright © 2020-2023  润新知