OStack程序员社区-中国程序员成长平台

标题: ios - iPhone 4 iOS 7 Tableview 滚动时滞后 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 05:32
标题: ios - iPhone 4 iOS 7 Tableview 滚动时滞后

我的 UITableview 出现延迟问题。 iPhone 5 上没有任何问题,并且在我开始在 NSDictionary 中缓存图像后,带有 iOS 6 的 iPhone 4 变得非常敏感。 但是,问题仍然存在于装有 iOS 7 的 iPhone 4 上。

我在这里阅读了一些关于使 View 不透明的提示,但没有帮助。除了标签之外,我的所有 View 都是不透明的(因为如果它们不透明,它们就会填充,这对我的目的不起作用)

我确实从 Storyboard 中加载了背景图片,你们知道这是否会影响性能吗? Storyboard在加载图像时效率低吗?

您还有其他提高 UITableView 性能的技巧吗?

提前致谢!

请求的一些代码,这些是单元格上的元素:http://imgur.com/Dcif6QE

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath {
HomeListCell *cell;

if([self.eventList lastObject])
{
    static NSString *CellIdentifier = @"HomeListCell";
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.userInteractionEnabled = YES;
    cell.event = [self.eventList objectAtIndex:indexPath.row];
    cell.parent = self;

    if([cell.event.event.event_type count] != 0)
    {
        Event_Type *eventType = [cell.event.event.event_type firstObject];
        NSString *imageName = @"HomeList_Type";
        imageName = [imageName stringByAppendingString:eventType.name];
        cell.eventTypeImage.image = [self.imageDict objectForKey:imageName];
    }

    //Laad de images hier uit de cache om scroll performance te verbeteren
    int score = [cell.event.rating intValue];
    [cell moveView:cell.ratingNumber duration:0.0 curve:UIViewAnimationCurveLinear x:0.0 y:0.0];
    if(score > 0)
    {
        cell.ratingImage.image = [self.imageDict objectForKey"HomeList_plus"];
    }
    else if(score == 0)
    {
        cell.ratingImage.image = nil;
        [cell moveView:cell.ratingNumber duration:0.0 curve:UIViewAnimationCurveLinear x:0.0 y:-10.0];
    }
    else
    {
        cell.ratingImage.image = [self.imageDict objectForKey"HomeList_min.png"];
        score = -score;
    }
    cell.ratingNumber.text = [NSString stringWithFormat"%d", score];

    [cell styleSelf];
}

而 styleSelf 有这样的代码:

-(void) styleSelf {
LocationManager *locationManager = [LocationManager sharedInstance];

//Tekens die verandert moeten worden
NSCharacterSet *notAllowedY = [NSCharacterSet characterSetWithCharactersInString"ÿ"];
NSString *resultString = [[event.event.name componentsSeparatedByCharactersInSet:notAllowedY] componentsJoinedByString"y"];

//Afstand berekening
double eventLong = [self.event.location.address.gps_long doubleValue];
double eventLat = [self.event.location.address.gps_lat doubleValue];
CLLocation* locatie = [[CLLocation alloc]initWithLatitude:eventLat longitude:eventLong];

//Date + time
NSString *eventDate = event.opening;
eventDate = [eventDate stringByReplacingOccurrencesOfString"T" withString" "];
NSDate *theDate;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];

if([eventDate hasSuffix"Z"])
{
    [dateFormatter setDateFormat"yyyy-MM-dd HH:mm:ssZ"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName"UTC"]];
    theDate = [dateFormatter dateFromString:eventDate];
}
else
{
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    theDate = [dateFormatter dateFromString:eventDate];
}
[dateFormatter setDateFormat:@"HH:mm"];
self.timeNumberLabel.text = [dateFormatter stringFromDate:theDate];
self.timeNumberAfscheurLabel.text = [dateFormatter stringFromDate:theDate];

[dateFormatter setDateFormat:@"MM-dd"];
if ([[dateFormatter stringFromDate:theDate] isEqualToString:[dateFormatter stringFromDate:[NSDate date]]])
{
    self.timeWhenLabel.text = NSLocalizedString(@"HomeList-Vandaag", nil);
    self.timeWhenAfscheurLabel.text = NSLocalizedString(@"HomeList-Vandaag", nil);
}
else
{
    [dateFormatter setDateFormat:@"MM"];
    NSString *maand = [dateFormatter stringFromDate:theDate];
    NSString *monthName = NSLocalizedString([@"Maand-" stringByAppendingString: maand], nil);
    [dateFormatter setDateFormat:@"d"];
    NSString *dag = [dateFormatter stringFromDate:theDate];

    NSString *DatumString = [[dag stringByAppendingString:@" "]stringByAppendingString:monthName];

    self.timeWhenLabel.text = [@"  " stringByAppendingStringatumString];
    self.timeWhenAfscheurLabel.text = [@"  " stringByAppendingStringatumString];
}

//De cell vormen of de user gaat of niet
if([event.user_attends_event count] == 0)
{
    [self moveView:self.nietAfgescheurdKnop duration:0 curve:UIViewAnimationCurveLinear x:0.0 y:0.0];
    [self moveView:self.timeNumberAfscheurLabel duration:0 curve:UIViewAnimationCurveLinear x:0.0 y:0.0];
    [self moveView:self.timeWhenAfscheurLabel duration:0 curve:UIViewAnimationCurveLinear x:0.0 y:0.0];
}
else
{
    [self moveView:self.nietAfgescheurdKnop duration:0 curve:UIViewAnimationCurveLinear x:50.0 y:0.0];
    [self moveView:self.timeNumberAfscheurLabel duration:0 curve:UIViewAnimationCurveLinear x:50.0 y:0.0];
    [self moveView:self.timeWhenAfscheurLabel duration:0 curve:UIViewAnimationCurveLinear x:50.0 y:0.0];
}

self.event.userDistance = [locationManager getDistanceBetween:locatie];
if([self.event.userDistance isEqualToString:@"GPS error"])
{
    self.distanceNumberLabel.text = NSLocalizedString(@"Extras-GPS", nil);
    self.distanceTypeLabel.text = NSLocalizedString(@"Extras-UIT", nil);
    self.distanceNumberLabel.textColor = [UIColor grayColor];
    self.distanceTypeLabel.textColor = [UIColor grayColor];
}
else
{
    NSString *placehold = self.event.userDistance;
    placehold = [placehold stringByReplacingOccurrencesOfString:@"." withString:@","];
    self.distanceNumberLabel.text = placehold;
    self.distanceTypeLabel.text = NSLocalizedString(@"Extras-Km", nil);
    self.distanceNumberLabel.textColor = [UIColor blackColor];
    self.distanceTypeLabel.textColor = [UIColor blackColor];
}

// Configure the cell...
self.titleLabel.text = resultString;
self.tagsLabel.text = [event getMetadataString];

}



Best Answer-推荐答案


你的罪魁祸首是 NSDateFormatter。这是一个要创建的超重对象。您应该在某处创建它的单个版本并重复使用它,自由设置属性(格式、时区等)。

使用 Instruments -> Time Profiler 来准确查看哪些方法占用了主线程上的时间也是一个好主意。

关于ios - iPhone 4 iOS 7 Tableview 滚动时滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20144626/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4