【问题标题】:UITableView scrolling delayUITableView 滚动延迟
【发布时间】:2013-12-06 05:16:41
【问题描述】:

在@parcs 和@payal 的帮助下找到了解决方案- 这个链接有帮助-> http://www.appcoda.com/customize-table-view-cells-for-uitableview/

基本上,当我滚动时,表格视图会滞后,我浏览了很多帖子,但没有帮助-

UITableView in UIScrollview scrolling delay

Delayed UIImageView Rendering in UITableView

slow scrolling of UITableView

UITableView lags while scrolling

这是我在 cellForRowIndexpath 中使用的代码

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

        static NSString *CellIdentifier = @"WorkoutCell";

        WorkoutCell *cell = (WorkoutCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(!cell)
        {
            UIViewController *controller=[[UIViewController alloc] initWithNibName:CellIdentifier bundle:nil];
            cell=(WorkoutCell *)controller.view;
        }



        [[cell contentView] setBackgroundColor:[UIColor clearColor]];
        [[cell backgroundView] setBackgroundColor:[UIColor clearColor]];
        [cell setBackgroundColor:[UIColor clearColor]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        //diplaying Workout Data

        [cell setupFont];
        cell.lblPoint.text=[NSString stringWithFormat:@"%@",[[arrTblData objectAtIndex:indexPath.row]valueForKey:@"point"]];
        cell.lblReps.text=[[arrTblData objectAtIndex:indexPath.row]valueForKey:@"reps"];
        cell.lblWorkoutCategory.text=[[arrTblData objectAtIndex:indexPath.row]valueForKey:@"workout"];
        cell.lblWeight.text=[[arrTblData objectAtIndex:indexPath.row]valueForKey:@"weight"];
        cell.lblSets.text=[[arrTblData objectAtIndex:indexPath.row]valueForKey:@"sets"];
        //Formatting Date
         cell.lblPostedDate.text=[NSString stringWithFormat:@"Posted On: %@",[self formatDate:[[arrTblData objectAtIndex:indexPath.row]valueForKey:@"time"]]];


        return cell;

    }

从 cellForRowIndexpath 调用的函数 SetupFont -

(void)setupFont
    {
        self.lblPoint.font=DEFAULT_FONT(13);
        self.pointLblScreen.font=DEFAULT_FONT(13);
        self.lblPostedDate.font=DEFAULT_FONT(13);
        self.lblReps.font=DEFAULT_FONT(13);
        self.lblSets.font=DEFAULT_FONT(13);
        self.lblWeight.font=DEFAULT_FONT(13);
        self.lblWorkoutCategory.font=DEFAULT_FONT(18);
        self.lblWorkoutCategory.strokeColor=kStrokeColor2;
        self.lblWorkoutCategory.strokeSize = kStrokeBigSize;
        for(UILabel *lbl in [self subviews])
        {


            if(lbl.tag==9)
            {
                lbl.font=DEFAULT_FONT(13);
            }

        }


    }

从 cellForRowIndexpath 调用的格式化日期函数

#pragma mark - Format Date Function
-(NSMutableString*)formatDate:(NSString*)date
{
    NSMutableString *formattedDate=[[NSMutableString alloc]init];
    NSUInteger p = 0;
    NSUInteger length = [date length];
    while ( p < length) {
        unichar ch = [date characterAtIndex: p];
        if (ch!=' ')
        {
            if(ch=='-')
            {
                ch='/';
            }
            NSString *appendString = [NSString stringWithFormat:@"%c",ch];

            [formattedDate appendString:appendString];
            p++;
        }
        else
        {
            break;
        }
    }

    return formattedDate;

}

【问题讨论】:

  • 这段代码最不寻常的方面(以及问题的可能原因)是分配一个视图控制器,然后将它的视图转换为一个 tableviewcell。通过注释掉所有其他配置步骤,您可以学到很多东西,也许只留下这样的一行:cell.lblPoint.text=[NSString stringWithFormat:@"%d", indexPath.row];
  • 为什么要将视图控制器分配给单元格,这会使您的应用程序变慢..
  • @danh- 好吧,我在大多数应用程序中都使用过这个过程,但直到现在我还没有发现这种问题。
  • 好的,我正在检查...我会告诉大家的。
  • @danh 和 Bullet Raja——我如何替换这段代码 if(!cell) { UIViewController *controller=[[UIViewController alloc] initWithNibName:CellIdentifier bundle:nil];单元格=(WorkoutCell *)controller.view; }

标签: ios iphone objective-c uitableview


【解决方案1】:

这不是正确的做法:

WorkoutCell *cell = (WorkoutCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(!cell)
        {
            UIViewController *controller=[[UIViewController alloc] initWithNibName:CellIdentifier bundle:nil];
            cell=(WorkoutCell *)controller.view;
        }

取而代之的是子类 UITableViewCell 并在此处实例化该单元格,如下所示:

WorkoutCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"WorkoutCell" owner:self options:nil]; 
            cell = [topLevelObjects objectAtIndex:0];
        }

另外,将setupFont 方法移至UITableViewCell 子类。

【讨论】:

  • 这是给空白单元格,表示单元格没有出现
  • 你在使用xib吗?如果是,则使用以下方法实例化单元: cell = [[[NSBundle mainBundle]loadNibNamed:@"CustomCell" owner:nil options:nil] objectAtIndex:0];
  • 代替这个语句:cell = [[WorkoutCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  • 一个错误伙伴——由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[ setValue:forUndefinedKey:]:这个类不符合键值编码的键视图.'
  • 你在 UITableViewCell 子类中创建了@property 吗?如果你能发布一些代码就好了。
【解决方案2】:

您正在将 UIViewController 类型转换为 UITableviewCell。取而代之的是 UITableViewCell 类添加您想要的任何插座并使用它

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    静态 NSString *CellIdentifier = @"CustomCell";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    如果(单元格 == nil){

    cell = [[[NSBundle mainBundle]loadNibNamed:@"CustomCell" owner:nil options:nil] objectAtIndex:0];
    

    }

    // 在此处访问 CustomCell 变量

    cell.name.text = @"名称";

}

【讨论】:

  • 由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[ setValue:forUndefinedKey:]:此类不符合键值编码的键视图。”
  • 你必须继承 UITableviewCell 并取一个空的 xib 并添加 UItableviewcell 并将类名更改为你的 UItableviewCell 类名并创建插座。您可以在 cellforRowAtIndex 方法中使用的网点
【解决方案3】:

你使用

@interface WorkoutCell : UITableViewCell {}

然后是表格视图类

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString * const CellIdentifier = @"WorkoutCell";
WorkoutCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if( !cell )
{
    cell = [[WorkoutCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
}
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多