zoukankan      html  css  js  c++  java
  • IOS ——UI篇—— UITableView的常用属性及代理方法的用法总结

    在手机的APP应用中UITableView的使用是很常见的,其用法也很灵活,需要开发者熟练掌握:

    在此用实例的方法对UITableView的用法进行总结:

    设置横向的tableview
    设置tableview属性:
    _tableView.transform = CGAffineTransformMakeRotation(-M_PI / 2);
    设置横向的cell
    设置cell属性
    cell.contentView.transform = CGAffineTransformMakeRotation(M_PI / 2); 
     
     
    #import "ViewController.h"

    @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
     
        UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];//初始化一个tableView并且设置它的风格(      UITableViewStyleGrouped分组风格  UITableViewStylePlain一般风格)

        tableView.rowHeight = 80;//行高(默认44)
        tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;//分割线样式
       // tableView.separatorColor = [UIColor whiteColor];//分割线的颜色
       
       // UIView *view = [[UIView alloc] init];
       
        UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"]];
       
        //view.backgroundColor = [UIColor brownColor];
       
       // tableView.backgroundView = bgView;//给tableView添加背景,可以不设置背景视图的frame(默认充满整个屏幕)
       
        UIView *header = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 0, 20)];
        header.backgroundColor = [UIColor grayColor];
     
        tableView.tableFooterView =  //tableView底部视图
        tableView.tableHeaderView = header;//tableView顶部视图,其frame只有高有效(默认与屏幕等宽)
       
        tableView.dataSource = self;//代理
        tableView.delegate= self;//代理
       
        tableView.tableFooterView = [[UIView alloc] init];
       
        [self.view addSubview:tableView];
    }
     
    //给TableView分组(如果此方法不写,默认为一组(组标号从0开始))
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;//设置table的组数,(默认是一组)
    }
     
    //设置每组的行数
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    //Section组数
    //    if (section == 0) {    //可以设置每一组的行数(适用于每组行数不同的情况)(行标号从0开始)
    //        return 1;
    //    }else if (section == 1){
    //        return 10;
    //    }
        return 4;//设置每组的行数
    }

    //设置每行对应的cell
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    //indexPath包含了第几组:indexPath.section 和第几行:indexPath.row
       
        static NSString *identifier = @"Cell";//重(chong)用机制标识
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];//根据重用标识,到重用池找对应的cell
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];//创建一个cell,设置其样式以及其标识   identifier(标示符)
    (initWithStyle有四种样式1.UITableViewCellStyleDefault2. UITableViewCellStyleValue1 3. UITableViewCellStyleValue2 4. UITableViewCellStyleSubtitle)
        }
       
       // cell.backgroundColor = [UIColor clearColor]; 给某一行设置背景色  (cell默认的背景色为白色,将其清除才能显示出tableView得背景图)
        //cell.backgroundView  给某一行设置背景图
        cell.textLabel.text = [NSString stringWithFormat:@"第%zi行,第%zi组",indexPath.row,indexPath.section];//设置cell的文本信息(indexPath.row=3 代表标号为3的哪一行  indexPath.section=2代表标号为2的哪一组)
     
        cell.imageView.image = [UIImage imageNamed:@"58"];//设置每一行的图标(左侧的图标)
       
        return cell;//将设置好的cell返回
    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    @end
     
     
     
    UITableView的代理方法:
     
     
    #import "ViewController.h"

    @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];

        UITableView *_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
       
    }
    //设置组数
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 3;
    }
    //设置每组的行数
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        if (section == 0) {
            return 3;
        }else if (section == 1){
            return 4;
        }
        return 200;
    }
    //重用机制
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *identifier = @"Cell";
       
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];     //子标题风格
        }
       
        cell.textLabel.text = [NSString stringWithFormat:@"%zi组,%zi行",indexPath.section,indexPath.row];
        cell.detailTextLabel.text = @"详细内容";//每一行的标题;(小标题)
       
        if (indexPath.section == 0 && indexPath.row == 1) {
            cell.imageView.image = [UIImage imageNamed:@"1"];
        }
       
        if (indexPath.section == 2 && indexPath.row == 0) {
            cell.imageView.image = [UIImage imageNamed:@"2"];
        }
       
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//提示用户可点,点击之后跳至下级界面
       
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//提示用户可点,点击按钮(叹号)会有相关提示弹出,点击cell之后跳至下级界面(有叹号和向右的箭头)
       
        cell.accessoryType = UITableViewCellAccessoryCheckmark;//对勾
       
        cell.accessoryType = UITableViewCellAccessoryDetailButton;//提示用户可点,点击按钮(叹号)会有相关提示弹出(只有一个叹号)
       
        return cell;
       
        }

    //设置每组顶部的高度
    -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
        //每组顶部视图的高度
        return 20;
    }
    //设置每组底部的高度
    -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
        return 30;
    }

    //自定义每组头视图
    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
        //自定义每组头视图
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
        view.backgroundColor = [UIColor brownColor];
        return view;
    }
    //自定义每组尾部视图
    -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
        //自定义尾部视图
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
        view.backgroundColor = [UIColor purpleColor];
        return view;
     
    }

    //设置每行的高度(可以选择设置某一行的高度)
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        //设置每行的高度(可以选择设置某一行的高度)
        if (indexPath.row == 0) {
            return 80;
        }
        return 40;
    }

    //设置每一组的标题
    -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
        return [NSString stringWithFormat:@"第%zi组",section];
    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    @end
     
     
    点击cell后所触发的代理方法
     
    #import "ViewController.h"

    @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{
        UITableView *_tableView;
        NSMutableArray    *_array;//盛放要显示的东西
       
    }

    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
       
        _array = [NSMutableArray arrayWithObjects:@"111",@"222",@"333", nil];
       
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 375, 647) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
       
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor = [UIColor grayColor];
        button.frame = CGRectMake(300, 20, 60, 60);
        [button setTitle:@"+" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(addCell) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
    }
    //点击button,tableview的Cell个数+1
    -(void)addCell{
        NSLog(@"=====");
        [_array addObject:@"123"];
        [_tableView reloadData];//当tableView的数据源发生改变时,调用该方法,会更新tableView的显示,
    }
    //动态设置行数
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return _array.count;
    }

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *identifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
       
        cell.selectionStyle = UITableViewCellSelectionStyleDefault;//设置cell选中的背景色
       
        UIView *view = [[UIView alloc] initWithFrame:cell.frame];
        view.backgroundColor = [UIColor blueColor];
        cell.selectedBackgroundView = view;//自定义cell选中的背景
       
        cell.textLabel.text = _array[indexPath.row];

        cell.textLabel.highlightedTextColor = [UIColor whiteColor];//选中状态下,字体颜色
       
       // cell.accessoryView  自定义右侧视图(是view或其子类)
       
        return cell;
    }
    //当cell的accessoryStyle中包含信息按钮(叹号)时,点击按钮触发的方法
    -(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
       
    }
    //取消选中(选中被取消之后触发的方法)
    -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
       // NSLog(@"----%zi",indexPath.row);
    }

    //cell被选中触发的方法
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        NSLog(@"----%zi",indexPath.row);
       
        [tableView deselectRowAtIndexPath:indexPath animated:YES];//当cell被点击时,取消选中状态
    }

    //是否允许编辑
    -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
        return YES;
    }
     
    //提交修改动作(前提条件是允许编辑):  在执行删除之前,需要先移除数组中对应的元素,
    -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
       
        [_array removeObjectAtIndex:indexPath.row]; //删除数组中对应的数据
       
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle]; //删除表格中对应的行
    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    @end
     
     
    解决重用机制的bug(重复问题)
     
     
    //重用机制:当我们使用tableView时,系统只会创建屏幕中显示的cell个数+1,当cell滑出可视范围时,会将此Cell放入重用池,当有新的cell滑进可视范围时,会先到重用池找有没有可以使用的cell(根据标识),如果能找到,则拿出来直接用,否则再创建新的cell

    //(如果每一行都有内容,就不需要解决重用机制的bug)
    #import "ViewController.h"

    @interface ViewController ()<UITableViewDataSource>

    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];

        UITableView *table = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        table.dataSource = self;
        [self.view addSubview:table];
       
    }

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return 40;
    }

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
       // NSString *identifier = [NSString stringWithFormat:@"cell%zi%zi",indexPath.section,indexPath.row];//给每行设置不同的标识(可以解决)

        static NSString *identifier = @"Cell";
       
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        //UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];处理重用bug很不友好的方式,不建议使用(消耗内存)
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
       
        cell.textLabel.text = nil;//先清理,再使用(常用方法)
       
        if (indexPath.row == 1) {
            cell.textLabel.text = @"1212";
        }
       
        return cell;
    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    @end
     
     
    感谢您的访问! 若对您有帮助或有兴趣请关注博客:http://www.cnblogs.com/Rong-Shengcom/
  • 相关阅读:
    Day1-CSS-下拉菜单
    scau 1138 代码等式
    SCAU-1076 K尾相等数
    勾股数专题-SCAU-1079 三角形-18203 神奇的勾股数(原创)
    SCAU-1144 数星星-HDU-1166-树状数组的应用
    NodeJs Fs模块
    Node核心模块
    flutter环境配置
    纯CSS制作图形效果
    CSS3选择器
  • 原文地址:https://www.cnblogs.com/Rong-Shengcom/p/4992032.html
Copyright © 2011-2022 走看看