zoukankan      html  css  js  c++  java
  • UITableView

    在IOS开发中,最常用的UI控件,应该就是UITableView了,在这里简单的对其进行介绍.
    UITableView有数据源方法和代理方法,数据源方法是将模型数据显示到视图上,而代理方法是对TableView进行操作.
    初始化方法:

    //UITableViewStylePlain 平板格式
    //UITableViewStyleGrouped 分组格式
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    //常用属性
    //设置表格可编辑
    tableView.editing = YES;
    
    //设置tableView自带的分割线
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //设置tableView的行高(全部行)
    tableView.rowHeight = 50;

    下面分别介绍UITableView的数据源方法和代理方法:
    数据源方法:

    //成为自己的数据源
    tableView.dataSource = self;
    
    #pragma mark - UITableViewDataSource 数据源方法
    /** 每个分组中的行数(必须实现) */
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return self.foods.count;
    }
    
    /** 每一行显示的内容(必须实现) */
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *ID = @"food";
        //取缓冲池查找可重用的单元格
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (!cell) {
            //UITableViewCellStyleDefault,  默认类型, 标题 + 图像
            //UITableViewCellStyleValue1,   标题 + 明细 + 图像
            //UITableViewCellStyleValue2,   不显示图像
            //UITableViewCellStyleSubtitle  标题 + 明细 + 图像
            //实例化新的单元格
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
        cell.textLabel.text = self.foods[indexPath.row];
        return cell;
    }
    
    //下面的方法都是可选的方法
    ///** 设置表格的组数 */
    //- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    //    return 2;
    //}
    
    /** 设置组标题 */
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
        return @"Header";
    }
    /** 设置组尾标 */
    - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
        return @"Footer";
    }

    代理方法:

    //成为自己的代理
    tableView.delegate = self;
    
    #pragma mark - UITableViewDelegate 代理方法
    /** 选中哪行 */
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        NSLog(@"%@", self.foods[indexPath.row]);
    }
    
    /** 删除行 */
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
        if (editingStyle == UITableViewCellEditingStyleDelete) {//删除
            //从数组中删除
            [self.foods removeObjectAtIndex:indexPath.row];
            //表格局部刷新
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        } else if(UITableViewCellEditingStyleInsert){//插入
            [self.foods addObject:@"满汉全席"];
            [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        }
    }
    
    /** 返回编辑的类型 (默认是UITableViewCellEditingStyleDelete)*/
    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
        //这个返回值,就使得TableView可选中(用于多选,批量操作)
        return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
    }
    
    /**
     *  表格的移动
     */
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
        // 1.从源数据中取出
        NSString *footName = self.foods[sourceIndexPath.row];
    
        // 2.然后删除
        [self.foods removeObjectAtIndex:sourceIndexPath.row];
    
        // 3.再插入到数组的目标位置
        [self.foods insertObject:footName atIndex:destinationIndexPath.row];
    
        // 4.刷新表格
        [tableView reloadData];
    }
    
    /**
     *  设置tableView的行高(每行可以单独设置)
     */
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        if (indexPath.row % 2){
            return 50;
        }else{
            return 80;
        }
    }

    补充:

    // 数据多,应该往上滚动
    NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:self.foods.count - 1 inSection:0];
    
    [self.tableView scrollToRowAtIndexPath:lastIndex atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    
    /**
     *  添加索引
     */
    - (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
        return nil;
    }
    不积跬步,无以至千里;不积小流,无以成江海。
  • 相关阅读:
    WCF 第三章 信道
    WCF 第三章 信道形状
    对单表数据生成insert语句
    WCF 第二章 契约 定义类的层次结构
    WCF 第三章 信道 总结
    Win32类型和.net类型的对应表
    用一条SQL语句实现斐波那契数列
    WCF 第一章 基础 为一个ASMX服务实现一个WCF客户端
    WCF 第二章 契约 数据契约版本
    WCF 第二章 契约 异步访问服务操作
  • 原文地址:https://www.cnblogs.com/xiaocai-ios/p/7779782.html
Copyright © 2011-2022 走看看