Slow UITableView滚动

问题描述:

编辑-----我在英文记录上做了相同的代码,滚动速度保持平常一样快,而且工作正常,但是当我提取阿拉伯数据时,滚动再次变慢。这是阿拉伯数据的问题吗?Slow UITableView滚动

我有约100的记录和我的tableview滚动非常缓慢。任何人都可以告诉我这段代码有什么问题,为什么iam会慢速滚动?

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

    GreetingAppDelegate *appdelegate = (GreetingAppDelegate *)[[UIApplication sharedApplication]delegate]; 
    DBSetter *product = (DBSetter *)[appdelegate.myrecords objectAtIndex:indexPath.row];  

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     cell.textLabel.font=[UIFont fontWithName:@"Arial" size:16.0]; 
    } 


    CGRect a=CGRectMake(8, 0, 307, 59); 
    UIImageView *aImg=[[UIImageView alloc] initWithFrame:a]; 
    UIImageView *bImg=[[UIImageView alloc] initWithFrame:a]; 

    //if(indexPath.row%2==0) 
    aImg.image=[UIImage imageNamed:@"cell-arrow.png"]; 


    //else { 
    // aImg.image=[UIImage imageNamed:@"CellHighlight.png"]; 
    //} 



    bImg.image=[UIImage imageNamed:@"cell-arrow-h.png"]; 
    [aImg setContentMode:UIViewContentModeScaleToFill]; 
    [bImg setContentMode:UIViewContentModeScaleToFill]; 
    cell.backgroundView=aImg; 
    cell.selectedBackgroundView=bImg; 
    [aImg release]; 
    [bImg release]; 


    NSString *tempStr=[NSString stringWithFormat:@"%@",product.tempdesc]; 

    //int stringlength=[tempStr length]; 
    //[[tempStr stringByReplacingOccurrencesOfString:@"<Dear User>" withString:@" "] substringWithRange:NSMakeRange(0, 20)]; 

    //if (stringlength >20) { 
    //cell.textLabel.text = [NSString stringWithFormat:@"%@...", [[tempStr stringByReplacingOccurrencesOfString:@"<Dear user>" withString:@""] substringWithRange:NSMakeRange(0, 30)]]; 
    //} 
    //else { 
     cell.textLabel.text = tempStr; 
    //} 


    if(appdelegate.lang==2) 

    { 
     [cell setTextAlignment:UITextAlignmentRight]; 
aImg.transform = CGAffineTransformMakeScale(-1, 1); 
     bImg.transform = CGAffineTransformMakeScale(-1, 1); 
    } 

    return cell; 
} 

感谢每一个人的回复......我只是想出了我的自我。我只是使用长度和范围来使它工作。我使用的代码如下,对于可能遭遇同样问题的代码。

NSString *tempStr=[NSString stringWithFormat:@"%@",product.tempdesc]; 

    int stringlength=[tempStr length]; 
    if (stringlength >20) { 
    cell.textLabel.text = [NSString stringWithFormat:@"%@...", [[tempStr stringByReplacingOccurrencesOfString:@"<Dear user>" withString:@""] substringWithRange:NSMakeRange(0, 30)]]; 
    } 
    else { 
    cell.textLabel.text = tempStr; 
    } 

我看到的第一件事:你没有利用回收利用!

if (cell == nil)块中尽可能地做。避免在每次滚动项目时创建图像视图和其他类似的东西。

我看到的再生细胞唯一有意义的代码是cell.textLabel.text = tempStr;系列。

+0

NOP仍然缓慢滚动:( – 2011-05-24 14:47:06

一个可能的原因可能是你正在创建imageView的单元格创建块之外。试试这个

if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    cell.textLabel.font=[UIFont fontWithName:@"Arial" size:16.0]; 
    CGRect a=CGRectMake(8, 0, 307, 59); 
    UIImageView *aImg=[[UIImageView alloc] initWithFrame:a]; 
    UIImageView *bImg=[[UIImageView alloc] initWithFrame:a]; 

    aImg.image=[UIImage imageNamed:@"cell-arrow.png"]; 

    bImg.image=[UIImage imageNamed:@"cell-arrow-h.png"]; 
    [aImg setContentMode:UIViewContentModeScaleToFill]; 
    [bImg setContentMode:UIViewContentModeScaleToFill]; 
    cell.backgroundView=aImg; 
    cell.selectedBackgroundView=bImg; 
    [aImg release]; 
    [bImg release]; 
} 
+0

NOP仍然缓慢滚动:( – 2011-05-24 14:47:54

您是否尝试过使用该图片作为背景进行自定义TableViewCell?您可以在自定义TableViewCell的setSelected函数中选择后更改背景。

+0

我只是编辑的问题,检查,请.. – 2011-05-25 06:32:59