0

我的 tableviewcontroller 存在很大的性能问题。滚动非常缓慢。我在 didSelectRowAtIndexPath 方法上创建了一个 NSLOG,我意识到这在我所做的每一次滚动上都会被调用。应该是这样的?

我在这张表上进行了搜索,我有一些逻辑,因为数据取决于 json 响应。您可以在此处检查此方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        //NSLog(@" scroll");
        // Configure the cell...
    static NSString *CellIdentifier = @"contactCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    UILabel *nameLabel = (UILabel *)[cell viewWithTag:1];
    UILabel *workPlaceLabel = (UILabel *)[cell viewWithTag:2];

    if(searching)
    {
            //NSLog(@" copyListOfItems: %@",copyListOfItems);
        NSString*lastName=[[[copyListOfItems objectAtIndex:indexPath.row]objectForKey:@"Contact"]objectForKey:@"lastname"];
        if(lastName==nil)
        {
            lastName=@" ";
        }
        NSString*firstName=[[[copyListOfItems objectAtIndex:indexPath.row]objectForKey:@"Contact"]objectForKey:@"firstname"];
        if(firstName==nil)
        {
            NSArray*phonesArray=[[[copyListOfItems objectAtIndex:indexPath.row]objectForKey:@"Contact"]objectForKey:@"phone"];
            NSLog(@"NUMERO TELEFONE %d",[phonesArray count]);

            if([phonesArray count]>0)
            {
                NSString*phoneNumber=[[[copyListOfItems objectAtIndex:indexPath.row]objectForKey:@"Contact"] objectForKey:@"Phone"];
                nameLabel.text=phoneNumber;
            }else{
                nameLabel.text=[[[copyListOfItems objectAtIndex:indexPath.row]objectForKey:@"Contact"] objectForKey:@"Current"];
                workPlaceLabel.text=@"";
            }

        }else{
            NSString *stringName= [NSString stringWithFormat:@"%@ %@", firstName, lastName];
            nameLabel.text=stringName;
            workPlaceLabel.text=[[[copyListOfItems objectAtIndex:indexPath.row]objectForKey:@"Contact"] objectForKey:@"Current"];
        }
    }
    else {
            //NSLog(@" _contactsArray: %@",_contactsArray);
        NSString*lastName=[[[_contactsArray objectAtIndex:indexPath.row]objectForKey:@"Contact"] objectForKey:@"Lastname"];
        if(lastName==nil)
        {
            lastName=@" ";
        }
        NSString*firstName=[[[_contactsArray objectAtIndex:indexPath.row]objectForKey:@"Contact"] objectForKey:@"Firstname"];
        if(firstName==nil)
        {
            NSArray*phonesArray=[[[_contactsArray objectAtIndex:indexPath.row]objectForKey:@"Contact"] objectForKey:@"Phone"];
                //NSLog(@"NUMERO TELEFONE %d",[phonesArray count]);

            if([phonesArray count]>0)
            {
                NSString*phoneNumber=[[[[_contactsArray objectAtIndex:indexPath.row]objectForKey:@"phone"] objectAtIndex:0]objectForKey:@"phonenumber"];
                nameLabel.text=phoneNumber;
            }else{
                nameLabel.text=[[[_contactsArray objectAtIndex:indexPath.row]objectForKey:@"Contact"] objectForKey:@"Current"];
                workPlaceLabel.text=@"";
            }
        }else{
            NSString *stringName= [NSString stringWithFormat:@"%@ %@", firstName, lastName];
            nameLabel.text=stringName;
           if([[[_contactsArray objectAtIndex:indexPath.row]objectForKey:@"Contact"] objectForKey:@"Current"])
            {
            workPlaceLabel.text=[[[_contactsArray objectAtIndex:indexPath.row]objectForKey:@"Contact"] objectForKey:@"Current"];

        }
        }
    }
        // Configure the cell...
    return cell;
}
4

2 回答 2

2

其中有很多不必要的调用可以从您的代码中删除。当您可以拨打一次时,所有这些重复的联系电话都需要时间来执行。就像在 if 语句的第一个分支中一样,您有如下调用:

NSString*lastName=[[[copyListOfItems objectAtIndex:indexPath.row]objectForKey:@"Contact"]objectForKey:@"lastname"];
NSString*firstName=[[[copyListOfItems objectAtIndex:indexPath.row]objectForKey:@"Contact"]objectForKey:@"firstname"];
NSArray*phonesArray=[[[copyListOfItems objectAtIndex:indexPath.row]objectForKey:@"Contact"]objectForKey:@"phone"];

您可以通过执行以下操作来压缩这些调用:

id contact = [[copyListOfItems objectAtIndex:indexPath.row]objectForKey:@"Contact"];
NSString *lastName=[contact objectForKey:@"lastname"];
NSString *firstName=[contact objectForKey:@"firstname"];
NSArray *phonesArray=[contact objectForKey:@"phone"];

不过,理想情况下,您将拥有一个自己的类,其中包含这些项目作为属性,因此您可以执行以下操作:

Contact *contact = (Contact *)[[copyListOfItems objectAtIndex:indexPath.row]objectForKey:@"Contact"];
NSString *lastName = contact.lastName;
NSString *firstName = contact.firstName;
NSArray *phonesArray = contact.phone;

编辑:如何进行异步图像加载

这是我过去使用占位符图像异步加载图像的方法。我使用的单元格是我编写的自定义类,但它应该让您了解如何操作。

// Setup the image view
UIImageView* imageView = cell.imageView;
imageView.image = [UIImage imageNamed:@"Loading.png"];

// Load the image asynchronously
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
    // Here you will want to make a call to your web server to get the image
    // and store it in a UIImage named image
    UIImage *image = // your code to get the image from the server

    // Only update if the cell is still on the screen
    if ([[tableView indexPathsForVisibleRows] containsObject:indexPath]) {
        // Have to update UI elements on the main thread
        dispatch_sync(dispatch_get_main_queue(), ^{
            [[cell imageView] setImage:image];
            [cell setNeedsLayout];
        });
    }
});
于 2012-10-25T12:06:21.687 回答
1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

为出现的每个单元格调用,因此如果您将逻辑排除在此循环之外会更好,因为它会在用户滚动时被多次调用,您可以将数组设置在此循环之外,只需替换此循环中的值方法

于 2012-10-25T10:14:59.350 回答