zoukankan      html  css  js  c++  java
  • UITableView 滚动时使用reloaddata出现 crash'-[__NSCFArray objectAtIndex:]: index (1) beyond bounds (0)' Crash

    例子:

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

    static NSString *CellIndentifier = @"Account";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier];
        
        //adjust the height for cell
        CGRect cellFrame = [cell frame];
        cellFrame.size.height = self.bgImageView.frame.size.height/2;
        
        //first time load the content
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIndentifier] autorelease];
        
        。。。。。。。。。

    else

      //这里主要是为了在滚动屏幕时候解决屏幕内容显示错误。但是却引发了这个问题,操作为向一个方向快速反复滚动就很容易crash,应该是reload后因为使用了dequeueReusableCellWithIdentifier导致table

    无法得到正确的应当显示的cell内容和个数,导致出错。

        [viewtable  reloadData];

    解决方法:

    static NSString *CellIndentifier = @"Account";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier];
        
        //adjust the height for cell
        CGRect cellFrame = [cell frame];
        cellFrame.size.height = self.bgImageView.frame.size.height/2;
        
        //first time load the content
        if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIndentifier] autorelease];
        
    //创建所有的子视图

    }else

    {

    //移除所有的子视图

     while ([cell.contentView.subviews lastObject] != nil)
                    {
                        [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];
                    }
    //重新添加子视图
     

    }

    这样做的效果上看不会因为删除子视图然后出现空白等待加载的不良效果

  • 相关阅读:
    js小案例---1.随机10不重复10数并排序2.一次输入10数并输出和
    23种设计模式-----转载
    类与类之间的关系-----转载
    设计模式六大原则-----转载
    配置JDK时环境变量path和JAVA_HOME的作用是什么?
    安装和配置JDK,并给出安装、配置JDK的步骤。
    1.Java为什么能跨平台运行?请简述原理。
    求圆的周长和面积
    java第一节课
    相关元素操作
  • 原文地址:https://www.cnblogs.com/lisa090818/p/3388165.html
Copyright © 2011-2022 走看看