1

我有一个UITableView包含所有单元格的图像,但所有图像都已经在单元格上(我没有设置我自己的图像)并且它们都是从项目中包含的图像加载的。这些不是从服务器或任何东西加载的图像。但是,tableview 的滚动仍然有些生涩。有什么办法可以加快速度吗?我有在这里生成单元格的功能:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // Post init, update with the fair info so it can attend/de-attend on its own
    CompaniesCustomCell *cell = [CompaniesCustomCell dequeOrCreateInTable:tableView];
    SectionInfo *sectionInfo = [sectionInfoArray objectAtIndex:indexPath.section];
    cell.fairDictionary = [sectionInfo.arrMDetails objectAtIndex:indexPath.row];
    [cell updateEventIcon:(NSString*)[[sectionInfo.arrMDetails objectAtIndex:indexPath.row] valueForKey:@"TYPE"]];

    cell.lblFairName.text = [[sectionInfo.arrMDetails objectAtIndex:indexPath.row] valueForKey:@"TITLE"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    // Set event start time, end time, and date
    NSString *newDate = [MyUtil convertToLocalTime:@"MMMM dd, yyyy" :[[sectionInfo.arrMDetails objectAtIndex:indexPath.row] valueForKey:@"START_TIME"]];        
    NSString *startString = [MyUtil convertToLocalTime:@"hh:mm a" :[[sectionInfo.arrMDetails objectAtIndex:indexPath.row] valueForKey:@"START_TIME"]];
    NSString *endString = [MyUtil convertToLocalTime:@"hh:mm a" :[[sectionInfo.arrMDetails objectAtIndex:indexPath.row] valueForKey:@"END_TIME"]];

    cell.lblFairDate.text = newDate;
    cell.lblFairStartTime.text = startString;
    cell.lblFairEndTime.text = endString;
    return cell;
}

有什么我做错了可能会减慢它的速度吗?

编辑

我在convertToLocalTime这里包含了函数的代码,因为我认为这可能是根本原因。但是......我真的很困惑如何优化它。我正在从服务器获取时间戳值(以 UTC 为单位),并将其转换为用户本地时区。这个过程真的那么慢吗?

// Converts given time string into the users current timezone
// based on the location of their phone
+(NSString *)convertToLocalTime:(NSString*)format :(NSString*)stringDate {

    // Get the date in the servers time zone
    NSDate *date = [MyUtil convertToServerDate:stringDate];

    // Convert the date to the local timezone
    NSDateFormatter *localFormat = [[NSDateFormatter alloc] init];
    [localFormat setDateFormat:format];
    NSString *localDate = [localFormat stringFromDate:date];

    [localFormat release];
    return localDate;
}

// Provides a NSDate object with the servers timezone set
+(NSDate *)convertToServerDate:(NSString*)stringDate {

    // First get the NSDate object in the servers timezone
    NSTimeZone *tz = [NSTimeZone timeZoneWithName:ServerTimezone];
    NSDateFormatter *inFormat = [[NSDateFormatter alloc] init];
    [inFormat setDateFormat:@"yyyyMMddHHmmss"];
    [inFormat setTimeZone:tz];
    NSDate *date = [inFormat dateFromString:stringDate];

    [inFormat release];
    return date;
}
4

3 回答 3

2

就像 EarlyRiser 所说,检查您的日期实用程序代码。但是,由于它是类方法,因此您需要创建新对象。

要尝试的另一件事是让自定义单元类更改标签。您可以从设置 fairDictionary 开始执行此操作。我假设您在自定义单元格的 .h 中的 fairDictionary 上使用了 @property。

在自定义单元格的 .m 中覆盖 fairDictionary 的 set 方法,因此在 @synthesize 的顶部执行:

@synthesize fairDictionary = _fairDictionary;

然后在 .m 文件的某处调用一个方法:

- (void)setFairDictionary:(NSDictionary *)fairDictionary {
_fairDictionary = fairDictionary;
[cell updateEventIcon:(NSString*)[_fairDictionary valueForKey:@"TYPE"];
self.lblFairName.text = [_fairDictionary valueForKey:@"TITLE"];

等等等等。所以不是表格数据源处理设置标题等。自定义类将在设置字典时进行。

希望这能有所帮助。

于 2012-10-25T15:41:35.653 回答
1

我最近做了相当多的 UITableView 优化,你最好的朋友是时间分析工具。如果您不知道如何使用它,那么您应该学习,它是一个救生员。

这是一个教程

您可能想要查看的一件事是您的日期实用程序代码。它在里面做什么?您是否每次调用它时都创建 NSCalendar 对象?这足以扼杀你的表现。但如果不进行分析,真的很难知道。

于 2012-10-25T04:02:16.910 回答
1

我在这个问题上遇到了很多困难,所以我非常清楚你的问题。UITableView 很生涩?我很惊讶所有的答案都指向日期函数。你可以把所有的东西都拿出来,它仍然会很生涩。

为什么?这是惰性图像解压缩。即使所有图像都来自应用程序包,ios 也会在它显示在屏幕上的那一刻执行解压缩。您必须在后台线程中手动解压缩图像。

解压不仅仅意味着实例化一个 UIImage 对象。它可能有点复杂。最好的解决方案是下载 SDWebImage 并使用随附的图像解压缩器。

要了解有关此问题的更多信息,请参阅: http: //www.cocoanetics.com/2011/10/avoiding-image-decompression-sickness/

于 2013-02-16T08:23:14.957 回答