2011-05-24 94 views
3

編輯-----我在英文記錄上做了相同的代碼,滾動速度保持平常一樣快,而且工作正常,但是當我提取阿拉伯數據時,滾動再次變慢。這是阿拉伯數據的問題嗎?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; 
} 

回答

0

感謝每一個人的回覆......我只是想出了我的自我。我只是使用長度和範圍來使它工作。我使用的代碼如下,對於可能遭遇同樣問題的代碼。

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; 
    } 
2

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

if (cell == nil)塊中儘可能地做。避免在每次滾動項目時創建圖像視圖和其他類似的東西。

我看到的再生細胞唯一有意義的代碼是cell.textLabel.text = tempStr;系列。

+0

NOP仍然緩慢滾動:( – 2011-05-24 14:47:06

3

一個可能的原因可能是你正在創建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

1

您是否嘗試過使用該圖片作爲背景進行自定義TableViewCell?您可以在自定義TableViewCell的setSelected函數中選擇後更改背景。

+0

我只是編輯的問題,檢查,請.. – 2011-05-25 06:32:59