Slow UITableView scrolling

2019-07-03 10:41发布

Edit----- i done the same code on english record scrolling speed remain fast as usual and its working fine, but when i am fetching Arabic data scrolling is slow again. is this a problem with arabic data???

I have records of about 100 and my tableview scrolling is very slow. can anyone tell me what is wrong with this code, and why iam getting slow scrolling?

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

4条回答
爷的心禁止访问
2楼-- · 2019-07-03 11:19

Have you tried making a custom TableViewCell with that image as background? You can change the background upon select in the setSelected function of the custom TableViewCell.

查看更多
疯言疯语
3楼-- · 2019-07-03 11:35

First thing I see: you're not taking advantage of the recycling!

Do as much as possible in the if (cell == nil) block. Avoid creating image views and other things like that each time an item scroll.

The only meaningful code for recycled cells I see is the cell.textLabel.text = tempStr; line.

查看更多
冷血范
4楼-- · 2019-07-03 11:37

Thanks every one for your replies... i just figure out it my self. i just use length and range to make it works. the code that i used is as follow, for the one who might suffer same problem.

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;
    }
查看更多
倾城 Initia
5楼-- · 2019-07-03 11:40

A posible cause could be you're creating the imageView's outside the cell creation block. Try this

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];
}
查看更多
登录 后发表回答