IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    UITableView的一些优化

    键盘上的舞者发表于 2015-05-25 10:40:23
    love 0

    上一个例子,如果存在很多的cell,苹果官方的生成方式是这样的。在当前视角内所有的cell都分配内存,当cell离开视角的时候,就会销毁。但是当你拖回去的时候,又得重新分配内存,这里就存在性能的问题。

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath这个方法里面是可以进行性能优化的。

    代码如下:

    //每个cell的详细内容
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        //每次移动的时候都会生成cell,可以进行优化
        //从缓冲池里面取出可以循环利用的cell,如果没有就alloc重新分配一个
        static NSString *ID=@"cc";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
        if(cell==nil)
        {
            cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        }
        //UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
        //取出相应行对应的商品
        Shop *shop=_shops[indexPath.row];
        //设置标题
        cell.textLabel.text=shop.title;
        //设置图片
        NSString *imageName=[NSString stringWithFormat:@"%@",shop.ico];
        cell.imageView.image=[UIImage imageNamed:imageName];
        //设置描述
        cell.detailTextLabel.text=shop.desc;
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }



沪ICP备19023445号-2号
友情链接