2015-04-25 69 views
0

我在UITableViewController中有一個自定義UITableViewCell,但是當Controller試圖將這個自定義單元出列時,它會花費很長時間(比如2000ms)。當呈現UITableViewController時出現意外的延遲

這行代碼引起該問題

KidsListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"kidsReuseIdentifier" forIndexPath:indexPath]; 

KidsListTableViewCell是一個定製的電池,其包括幾個UIButton S,一些UILabel s到顯示信息。和兩個委託方法。呈現該觀點的速度不應該太慢。順便說一句,自定義單元格中的所有信息基本都是靜態的。

這是UITableViewController的完整代碼。我將自定義視圖和常規視圖放在不同的部分,我不認爲這會導致問題。

#import "KidDetailTableViewController.h" 
#import "KidDetailHeaderTableViewCell.h" 
#import <AssetsLibrary/AssetsLibrary.h> 
#import "Helper.h" 

@interface KidDetailTableViewController() <KidDetailHeaderCellDelegate> 

@end 

@implementation KidDetailTableViewController 
{ 
    KidDetailHeaderTableViewCell *headerCell; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    [self.tableView registerNib:[UINib nibWithNibName:@"KidDetailHeader" bundle:nil] forCellReuseIdentifier:@"kidDetail"]; 

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"detailCell"]; 


    self.tableView.showsVerticalScrollIndicator = NO; 

} 


- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 

} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // Return the number of sections. 
    return 2; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    switch (section) { 
     case 0: 
      return 1; 
      break; 

     default: 
      return 10; 
      break; 
    } 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    if (indexPath.section == 0) { 
     if (!headerCell) { 


      headerCell = [tableView dequeueReusableCellWithIdentifier:@"kidDetail"]; 

      headerCell.delegate = self; 


      // Keep the background color for the cell when select. 
      headerCell.selectionStyle = UITableViewCellSelectionStyleNone; 

      headerCell.nicknameLabel.text = _kidNeedsToShow.nickname; 

      NSString *kidFullName = [NSString stringWithFormat:@"%@ %@ %@", _kidNeedsToShow.firstName, _kidNeedsToShow.midName, _kidNeedsToShow.lastName]; 

      kidFullName ? (headerCell.fullNameLabel.text = @"") : (headerCell.fullNameLabel.text = kidFullName); 


      // Set thumb image or use default 
      // if there isn't image, use default. 
      ALAssetsLibrary *library = [ALAssetsLibrary new]; 
      [library assetForURL:_kidNeedsToShow.photoURL resultBlock:^(ALAsset *asset) { 
       [headerCell.avatarImage setImage:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]]]; 
      } failureBlock:nil]; 
      return headerCell; 
     } 
     else return headerCell; 
    } 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"detailCell" forIndexPath:indexPath]; 
    return cell; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (indexPath.section == 0) { 
     return 290; 
    } 
    else return 60; 
} 

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (indexPath.section == 0) { 
     return 290; 
    } 
    else return 60; 
} 

- (void)didClickLeftButton:(UIButton *)leftButton { 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

@end 

我試圖把dequeueReusableCellWithIdentifier到一個不同的線程,它顯然是行不通的。

UPDATE:KidDetailHeaderTableViewCell.m

- (void)awakeFromNib { 
    // Initialization code 
    [_avatarImage.layer setCornerRadius:_avatarImage.frame.size.width/2]; 
    [_avatarImage.layer setBorderColor:[UIColor whiteColor].CGColor]; 
    [_avatarImage.layer setBorderWidth:1.0]; 
    [_avatarImage setClipsToBounds:YES]; 

} 

- (IBAction)leftButtonClicked:(UIButton *)sender { 
    if (self.delegate && [self.delegate respondsToSelector:@selector(didClickLeftButton:)]) { 
     [self.delegate didClickLeftButton:sender]; 
    } 
} 
- (IBAction)rightButtonClicked:(UIButton *)sender { 
    if (self.delegate && [self.delegate respondsToSelector:@selector(didClickRightButton:)]) { 
     [self.delegate didClickRightButton:sender]; 
    } 
} 

KidDetailHeaderTableViewCell.h

@protocol KidDetailHeaderCellDelegate <NSObject> 

@optional 
- (void)didClickLeftButton:(UIButton *)leftButton; 
- (void)didClickRightButton:(UIButton *)rightButton; 

@end 

@interface KidDetailHeaderTableViewCell : UITableViewCell 

@property (weak) id<KidDetailHeaderCellDelegate> delegate; 

@property (weak, nonatomic) IBOutlet UILabel *fullNameLabel; 
@property (weak, nonatomic) IBOutlet UIImageView *avatarImage; 
@property (weak, nonatomic) IBOutlet UILabel *nicknameLabel; 

@property (weak, nonatomic) IBOutlet UILabel *ageText; 
@property (weak, nonatomic) IBOutlet UILabel *ageLabel; 

@property (weak, nonatomic) IBOutlet UILabel *momentsStatistics; 
@property (weak, nonatomic) IBOutlet UILabel *momentsLabel; 

@property (weak, nonatomic) IBOutlet UIButton *rightButton; 
@property (weak, nonatomic) IBOutlet UIButton *leftButton; 

@end 

更新2: 截圖儀器 screenshot of instrument

用於小區的設置高度的代碼。我有兩個部分,第一部分實際上只用於標題,高度爲290

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (indexPath.section == 0) { 
     return 290; 
    } 
    else return 60; 
} 
+0

你可以從'KidDetailHeaderTableViewCell'添加代碼嗎? (初始化器,'-prepareForReuse',''awakeFromNib') – Moxy

+0

@Moxy更新。我只使用awakeFromNib並設置一個UIImageView從正方形到圓形。我試圖擺脫它,沒有工作。 – Alex

+0

您是否使用儀器查找2000毫秒的持續時間? – Moxy

回答

0

我的一個朋友指出,問題是由自定義字體引起的(是的!我使用的自定義字體在自定義視圖)。我仍然不確定這是爲什麼導致這個問題(這個字體被命名爲「英雄」),但是希望這會幫助別人。