zoukankan      html  css  js  c++  java
  • IOS UITabelView的cell

     一、Cell的重用原理

    iOS设备的内存有限,如果用UITableView显示成千上万条数据,就需要成千上万个UITableViewCell对象的话,那将会耗尽iOS设备的内存。要解决该问题,需要重用UITableViewCell对象

    重用原理:当 滚动列表时,部分UITableViewCell会移出窗口,UITableView会将窗口外的UITableViewCell放入一个对象池中,等待 重用。当UITableView要求dataSource返回UITableViewCell时,dataSource会先查看这个对象池,如果池中有未 使用的UITableViewCell,dataSource会用新的数据配置这个UITableViewCell,然后返回给UITableView, 重新显示到窗口中,从而避免创建新对象

    还有一个非常重要的问题:有时候需要 自定义UITableViewCell(用一个子类继承UITableViewCell),而且每一行用的不一定是同一种 UITableViewCell,所以一个UITableView可能拥有不同类型的UITableViewCell,对象池中也会有很多不同类型的 UITableViewCell,那么UITableView在重用UITableViewCell时可能会得到错误类型的 UITableViewCell

    解决方案:UITableViewCell 有个NSString *reuseIdentifier属性,可以在初始化UITableViewCell的时候传入一个特定的字符串标识来设置 reuseIdentifier(一般用UITableViewCell的类名)。当UITableView要求dataSource返回 UITableViewCell时,先通过一个字符串标识到对象池中查找对应类型的UITableViewCell对象,如果有,就重用,如果没有,就传 入这个字符串标识来初始化一个UITableViewCell对象

     
    二、重用代码
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        // 1.定义一个cell的标识

          static NSString *ID = @"mycell";

        // 2.从缓存池中取出cell

          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

        // 3.如果缓存池中没有cell

          if (cell == nil) {

            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];

        }

        // 4.设置cell的属性...

          return cell;

    }
     
     
    三、如何动态调整Cell高度
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     
        static NSString *CellIdentifier = @"Cell";
     
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
            label.tag = 1;
            label.lineBreakMode = UILineBreakModeWordWrap;
            label.highlightedTextColor = [UIColor whiteColor];
            label.numberOfLines = 0;
            label.opaque = NO; // 选中Opaque表示视图后面的任何内容都不应该绘制
            label.backgroundColor = [UIColor clearColor];
            [cell.contentView addSubview:label];
            [label release];
        }
     
        UILabel *label = (UILabel *)[cell viewWithTag:1];
        NSString *text;
        text = [textArray objectAtIndex:indexPath.row];
        CGRect cellFrame = [cell frame];
        cellFrame.origin = CGPointMake(0, 0);
     
        label.text = text;
        CGRect rect = CGRectInset(cellFrame, 2, 2);
        label.frame = rect;
        [label sizeToFit];
        if (label.frame.size.height > 46) {
            cellFrame.size.height = 50 + label.frame.size.height - 46;
        }
        else {
            cellFrame.size.height = 50;
        }
        [cell setFrame:cellFrame];
     
        return cell;
    }
     
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
    }



    四、分割线
    一般地,利用类似[tableView setSeparatorColor:[UIColor redColor]];语句即可修改cell中间分割线的颜色。那又如何用一个图片作为分割线背景呢?可以尝试如下:
    方法一:
    先设置cell separatorColor为clear,然后把图片做的分割线添加到自定义的custom cell上。

    方法二:
    在cell里添加一个像素的imageView后将图片载入进,之后设置tableView.separatorStyle = UITableViewCellSeparatorStyleNone

    五、自定义首行Cell与其上面导航栏间距

    tableView.tableHeaderView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,5,20)] autorelease];


    六、cell触发
    在自定义的btnClicked方法中判断出事件发生在UITableView的哪一个cell上。因为UITableView有一个很关键的方法indexPathForRowAtPoint,可以根据触摸发生的位置,返回触摸发生在哪一个cell的indexPath。而且通过event对象,正好也可以获得每个触摸在视图中的位置。
    // 检查用户点击按钮时的位置,并转发事件到对应的accessory tapped事件
    - (void)btnClicked:(id)sender event:(id)event
    {
         NSSet *touches = [event allTouches];
         UITouch *touch = [touches anyObject];
         CGPoint currentTouchPosition = [touch locationInView:self.tableView];
         NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];
         if(indexPath != nil)
         {
             [self tableView:self.tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
         }
    }

    这样,UITableView的accessoryButtonTappedForRowWithIndexPath方法会被触发,并且获得一个indexPath参数。通过这个indexPath参数,我们即可区分到底哪一行的附件按钮发生了触摸事件。

    - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
    {
    int *idx = indexPath.row;
    //这里加入自己的逻辑
    }
     
    七、cell的单行触发
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{}

           此方法是理处每一个cell被择选后的动作

     
  • 相关阅读:
    [每日短篇] 1C
    项目Alpha冲刺 Day12
    项目Alpha冲刺 Day12
    [转载]MVC中单用户登录
    GitLab
    Git 版本控制
    Jenkins持续集成
    Jenkins安装
    Docker 网络基础原理
    java中内存的使用
  • 原文地址:https://www.cnblogs.com/zhongxuan/p/4859032.html
Copyright © 2011-2022 走看看