2012-07-15 69 views
2

我加載從Parse.com後端數據,他們給我使用的可重複使用的電池的解決方案,但現在我仍然有加載速度的煩惱,這是編碼我在我的的tableview我有彌補我的細胞(ExploreStreamCustomCell.m)加載速度慢的tableview與可重複使用的細胞

- (ExploreStreamCustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
         object:(PFObject *)object 
{ 
    static NSString *CellIdentifier = @"ExploreStreamCustomCell"; 

    ExploreStreamCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[ExploreStreamCustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
               reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell 
    cell.listItemTitle.text = [object objectForKey:@"text"]; 
    cell.checkinsLabel.text = [NSString stringWithFormat:@"%@", [object objectForKey:@"checkins"]]; 
    cell.descriptionLabel.text = [object objectForKey:@"description"]; 


    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

    PFFile *listThumbnail = [object objectForKey:@"header"]; 
    cell.listViewImage.image = [UIImage imageNamed:@"loading_image_stream.png"]; // placeholder image 
    cell.listViewImage.file = listThumbnail; 
    [cell.listViewImage loadInBackground:NULL]; 

    return cell; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForNextPageAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [super tableView:tableView cellForNextPageAtIndexPath:indexPath]; 
    cell.textLabel.font = [cell.textLabel.font fontWithSize:kPAWWallPostTableViewFontSize]; 
    return cell; 
} 

一個子類如果我有在細胞中的//設置單元格的所有內容==零的速度非常快,但它顯示的3 9個獨特的數據行和3個獨特的內容單元重複3次?內ExploreStreamCustomCell.m

#import "ExploreStreamCustomCell.h" 

@implementation ExploreStreamCustomCell 

@synthesize listViewImage, 
iconLocation, 
iconPeople, 
iconCheckins, 
listItemTitle, 
locationLabel, 
peopleLabel, 
checkinsLabel, 
descriptionLabel, 
listItemView; 


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){ 
     //Initialization code 
     listItemView = [[UIView alloc] init]; 
     listViewImage = [[PFImageView alloc] init]; 
     iconLocation = [[UIImageView alloc] init]; 
     iconPeople = [[UIImageView alloc] init]; 
     iconCheckins = [[UIImageView alloc] init]; 
     listItemTitle = [[UILabel alloc] init];  
     locationLabel = [[UILabel alloc] init]; 
     peopleLabel = [[UILabel alloc] init]; 
     checkinsLabel = [[UILabel alloc] init]; 
     descriptionLabel = [[UILabel alloc] init]; 

     listViewImage.image = [UIImage imageNamed:@"nachtwacht_list_formaat.png"]; 
     iconLocation.image = [UIImage imageNamed:@"icon_magenta_location.png"]; 
     iconPeople.image = [UIImage imageNamed:@"icon_magenta_people.png"]; 
     iconCheckins.image = [UIImage imageNamed:@"icon_magenta_checkins.png"]; 
     listItemTitle.text = @"text"; 
     locationLabel.text = @"0,7 km"; 
     peopleLabel.text = @"34"; 
     checkinsLabel.text = @"61"; 
     descriptionLabel.text = @"Description text."; 

     [self.contentView addSubview:listItemView]; 
     [self.contentView addSubview:listViewImage]; 
     [self.contentView addSubview:iconLocation]; 
     [self.contentView addSubview:iconPeople]; 
     [self.contentView addSubview:iconCheckins]; 
     [self.contentView addSubview:listItemTitle]; 
     [self.contentView addSubview:locationLabel]; 
     [self.contentView addSubview:peopleLabel]; 
     [self.contentView addSubview:checkinsLabel]; 
     [self.contentView addSubview:descriptionLabel]; 
    } 
    return self; 
} 

- (void)layoutSubviews { 
    [super layoutSubviews]; 
    CGRect contentRect = self.contentView.bounds; 
    CGFloat boundsX = contentRect.origin.x; 
    CGRect frame; 

    frame= CGRectMake(boundsX+0 , 33, 280, 124); 
    listViewImage.frame = frame; 
    listViewImage.contentMode = UIViewContentModeScaleAspectFill; 
    listViewImage.layer.masksToBounds = YES; 

    //listViewImage.backgroundColor = [UIColor lightGrayColor]; 

    frame= CGRectMake(boundsX+20 , 164, 12, 18); 
    iconLocation.frame = frame; 
    //iconLocation.backgroundColor = [UIColor lightGrayColor]; 

    frame= CGRectMake(boundsX+102 , 164, 24, 18); 
    iconPeople.frame = frame; 
    //iconPeople.backgroundColor = [UIColor lightGrayColor]; 

    frame= CGRectMake(boundsX+193 , 164, 20, 16); 
    iconCheckins.frame = frame; 
    //iconLocation.backgroundColor = [UIColor lightGrayColor]; 

    frame= CGRectMake(boundsX+0 , 0, 280, 33); 
    listItemView.frame = frame; 
    listItemView.backgroundColor = [UIColor colorWithRed:0.749 green:0.000 blue:0.243 alpha:1.000]; 


    frame= CGRectMake(boundsX+20 , 3, 240, 29); 
    listItemTitle.frame = frame; 
    //listItemTitle.textColor = [UIColor colorWithRed:250.0f/255.0f green:194.0f/255.0f blue:9.0f/255.0f alpha:0.8f]; 
    listItemTitle.textAlignment = UITextAlignmentLeft; 
    listItemTitle.font = [UIFont boldSystemFontOfSize:15]; 
    listItemTitle.textColor = [UIColor whiteColor]; 
    listItemTitle.backgroundColor = [UIColor clearColor]; 
    listItemTitle.lineBreakMode = UILineBreakModeTailTruncation; 
    //listItemTitle.backgroundColor = [UIColor orangeColor]; 

    frame= CGRectMake(boundsX+40 , 164, 57, 21); 
    locationLabel.frame = frame; 
    locationLabel.textAlignment = UITextAlignmentLeft; 
    locationLabel.font = [UIFont boldSystemFontOfSize:12]; 
    locationLabel.textColor = [UIColor colorWithRed:0.749 green:0.000 blue:0.243 alpha:1.000]; 
    locationLabel.backgroundColor = [UIColor clearColor]; 
    locationLabel.lineBreakMode = UILineBreakModeTailTruncation; 
    locationLabel.numberOfLines = 1; 
    //locationLabel.backgroundColor = [UIColor redColor]; 

    frame= CGRectMake(boundsX+134 , 164, 57, 21); 
    peopleLabel.frame = frame; 
    peopleLabel.textAlignment = UITextAlignmentLeft; 
    peopleLabel.font = [UIFont boldSystemFontOfSize:12]; 
    peopleLabel.textColor = [UIColor colorWithRed:0.749 green:0.000 blue:0.243 alpha:1.000]; 
    peopleLabel.backgroundColor = [UIColor clearColor]; 
    peopleLabel.lineBreakMode = UILineBreakModeTailTruncation; 
    peopleLabel.numberOfLines = 1; 

    frame= CGRectMake(boundsX+221 , 164, 51, 21); 
    checkinsLabel.frame = frame; 
    checkinsLabel.textAlignment = UITextAlignmentLeft; 
    checkinsLabel.font = [UIFont boldSystemFontOfSize:12]; 
    checkinsLabel.textColor = [UIColor colorWithRed:0.749 green:0.000 blue:0.243 alpha:1.000]; 
    checkinsLabel.backgroundColor = [UIColor clearColor]; 
    checkinsLabel.lineBreakMode = UILineBreakModeTailTruncation; 
    checkinsLabel.numberOfLines = 1; 

    frame= CGRectMake(boundsX+0 , 189, 280, 55); 
    descriptionLabel.frame = frame; 
    descriptionLabel.textAlignment = UITextAlignmentLeft; 
    descriptionLabel.font = [UIFont systemFontOfSize:13]; 
    descriptionLabel.backgroundColor = [UIColor clearColor]; 
    descriptionLabel.lineBreakMode = UILineBreakModeTailTruncation; 
    descriptionLabel.numberOfLines = 3; 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated { 

    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 


/* 
- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 


// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
} 
*/ 

@end 
+0

通常,將所有代碼移動到if語句的單元格== nil分支中沒有意義。 – 2012-07-15 19:42:33

+0

你認爲哪裏會讓時間流逝?我會嘗試加入的NSLog之類的語句的NSLog(@ 「<%@:%d>」,[的NSString stringWithUTF8String:__ FILE__] lastPathComponent],__LINE__];如果和在我的mehtod內的時間花費到determin – 2012-07-15 19:45:20

+0

請提供ExploreStreamCustomCell的一些代碼,特別是 – 2012-07-15 19:45:49

回答

3

一個偉大的方式

編輯額外的代碼來探索你的代碼的效率低下是使用儀器的時間Profiler工具。 Time Profiler可以讓你看到你的代碼中每行都花了多少時間。

Time Profiler


我建議以下設置分析:

Profiler settings


從蘋果的人臉檢測示例應用程序:

Face Detection


然後,您可以雙擊任意一行(更高的百分比意味着更多的時間用於該方法調用),以便在代碼中查看在每個位置花了多少時間。

Code analysis


從這裏你就可以開始找出你正在低效,看看到底是什麼佔用了這麼多時間。祝你好運!

+0

奧克,thnx ...但我不知道在哪裏點擊看到你的屏幕上的代碼? 調用樹是空的,並且示例列表未顯示您的第3個屏幕截圖expample – 2012-07-16 11:05:53

+0

您是在真實設備上還是在模擬器上測試? – 2012-07-16 14:10:16

+0

在真實設備上,iphone 3GS – 2012-07-16 14:58:35