UITableView滚动延迟

时间:2013-12-06 05:16:41

标签: ios iphone objective-c uitableview

在@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;

    }

函数SetupFont从cellForRowIndexpath 调用      -

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

}

3 个答案:

答案 0 :(得分:0)

你正在将UIViewController类型转换为UITableviewCell.Instead of UITableViewCell类添加你想要的出口并使用它

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

    static NSString * CellIdentifier = @“CustomCell”;

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

    if(cell == nil){

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

    }

    //在这里访问CustomCell变量

    cell.name.text = @“名称”;

}

答案 1 :(得分:0)

这不是正确的方法:

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子类。

答案 2 :(得分:0)

您使用

@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];
}
}