zoukankan      html  css  js  c++  java
  • UITableView

    重用机制:当我们使用tableView时,系统只会创建屏幕中显示的cell个数+1,当cell滑出可视范围时,会将此Cell放入重用池,当有新的cell滑进可视范围时,会先到重用池找有没有可以使用的cell(根据标识),如果能找到,则拿出来直接用,否则再创建新的cell

    #import "ViewController.h"

     

    @interfaceViewController ()<UITableViewDataSource,UITableViewDelegate>

    {

        UITableView *tabel;

        

        NSMutableArray *array;

     

    }

    @end

     

    @implementation ViewController

     

    - (void)viewDidLoad {

        [superviewDidLoad];

        array = [NSMutableArrayarrayWithObjects:@"123"@"456",@"789", nil];

        tabel = [[UITableViewalloc]initWithFrame:self.view.boundsstyle:UITableViewStyleGrouped];//UITableViewStyleGrouped组与组之间有空隙UITableViewStylePlain是直接连在一起的

        tabel.delegate = self;

        tabel.dataSource = self;

        [self.view addSubview:tabel];

        tabel.rowHeight = 80;//设置tabel的行高

        //tabel.separatorStyle = UITableViewCellSeparatorStyleNone;//分割线样式,可以不写。默认有线

        tabel.separatorColor = [UIColorblueColor];//设置分割线的颜色

    //    UIImageView *view = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"2.jpg"]];

    //    tabel.backgroundView = view;//背景图片的加载,会被cell掩盖,慎用!!!!

    //    UIView *vc = [[UIView alloc]initWithFrame:CGRectMake(0, 120, 0, 150)];

    //    vc.backgroundColor = [UIColor grayColor];

    //    tabel.tableHeaderView = vc;//tabel顶部加载一个view,其frame只有高有效

        

        UIButton *button = [UIButtonbuttonWithType:UIButtonTypeCustom];

        button.backgroundColor = [UIColor grayColor];

        button.frame = CGRectMake(300, 20, 60, 60);

        [button setTitle:@"+"forState:UIControlStateNormal];

        [button addTarget:selfaction:@selector(addcell) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:button];

        

    }

    -(void) addcell

    {

        [array addObject:@"147"];

        [tabelreloadData];

     

    }

    -(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

    {

        return100;//每组顶部视图的高度

     

    }

    -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

        return30;//每组底部视图的高度

    }

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

        //设置每行的高度

        if (indexPath.row == 0) {

            return 80;

        }

        return 40;

    }

    //-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

    //{

    //    //自定义每组顶部视图,可以在此处给视图顶部添加一些控件

    //    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 40)];

    //    view.backgroundColor = [UIColor redColor];

    //    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;

    //    

    //}

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

    //{

    //

    //    return 3;//设置tabel的组数,默认是一组

    //}

    -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    {

    //        if (section == 0) {

    //            return 5;

    //        }else if (section == 1){

    //            return 7;

    //        }

    //    return 9;//设置tabel没组的行数

        return array.count;

     

    }

    //提交修改动作再执行删除之前,需要先移除数组中对应的元素,

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

    {

        [array removeObjectAtIndex:indexPath.row];

        [tabeldeleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];//删除行

     

    }

    //是否允许编辑

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

        returnYES;

    }

     

    //cell被选中

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

        NSLog(@"----%zi",indexPath.row);

        

        // [tableView deselectRowAtIndexPath:indexPath animated:YES];//cell被点击时,取消选中状态

    }

     

      -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {

       static NSString *inde = @"cell";

    // NSString *identifier = [NSString stringWithFormat:@"cell%zi%zi",indexPath.section,indexPath.row];//给每行设置不同的标识,来处理重用机制

        UITableViewCell *cell = [tabeldequeueReusableCellWithIdentifier:inde ];//根据重用标识,到重用池找对应的cell

       // UITableViewCell  *cell = [tabel cellForRowAtIndexPath:indexPath];//处理重用机制不好,不建议使用

        if (cell == nil) {

            cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:inde];//创建一个cell,设置其样式以及标识

        }

        cell.textLabel.text = @""; //先清理,在使用,解决重用机制

      // cell.textLabel.text = [NSString stringWithFormat:@"%zi行,第%zi",indexPath.row,indexPath.section];

    //    cell.imageView.image = [UIImage imageNamed:@"58"];

        

        cell.textLabel.text = array[indexPath.row];

        

        

        

    //    if (indexPath.row ==1) {

    //        cell.textLabel.text = @"123";//如果有多行,且没有赋满值,滑动时回出现标题重用

    //    }

        

        cell.detailTextLabel.text = @"详细信息";

        //给不同行不同列添加图片

        if (indexPath.section == 0&& indexPath.row == 1) {

            cell.imageView.image = [UIImage imageNamed:@"1"];

        }

        if (indexPath.section == 1 && indexPath.row ==1) {

            cell.imageView.image = [UIImage imageNamed:@"2"];

        }

        

       // cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;//提示用户可以点,点击后跳至下级界面

        

        //cell.accessoryType =UITableViewCellAccessoryDetailDisclosureButton;//提示用户可点,点击按钮(叹号)会有相关提示弹出,点击cell之后跳至下级界面

        

        cell.accessoryType = UITableViewCellAccessoryDetailButton;//提示用户可点,点击按钮(叹号)会有相关提示弹出

        

        return  cell;//将设置好的cell返回

    }

    - (void)didReceiveMemoryWarning {

        [superdidReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

     

    @end

  • 相关阅读:
    Eclipse中使用GIT提交文件至本地
    Eclipse中使用GIT更新项目
    Eclipse使用Git检出项目
    JQuery选择器排除某元素实现js代码
    如何在使用layer.prompt在输入值为空的情况下点击确定继续执行逻辑?
    怎样验证layer.prompt输入的值为数值型???
    使用ECharts制作图形时,如何设置指定图形颜色?
    JS中通过LayUI的layer.prompt弹出文本输入层,多个按钮回调获取输入值
    MAVEN环境配置
    【Linux】Tomcat安装及端口配置
  • 原文地址:https://www.cnblogs.com/lcl15/p/4973411.html
Copyright © 2011-2022 走看看