2013-08-19 84 views
0

我有一個UITableViewCell的子類,我遇到了滾動問題。所有的子視圖都被添加到故事板中的單元格中,除了一個UIView。我想根據條件添加這個UIView作爲單元格的子視圖。問題是,當單元格滾動到屏幕上時,UIView會再次添加到單元格中,或添加到錯誤的單元格中。這是我的代碼,你能告訴我我做錯了什麼嗎?UITableViewCell子視圖滾動時添加到錯誤的單元格

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    WavesFeedCell *cell = (WavesFeedCell *)[tableView dequeueReusableCellWithIdentifier:WavesFeedCellIdentifier forIndexPath:indexPath]; 

    cell.cellWaveObject = [self.wavesArray objectAtIndex:indexPath.row]; 

    //set the frames and text for the labels 
    cell.timeStampLabel.text = @"14m"; 
    cell.waveTextLabel.text = cell.cellWaveObject.waveString; 
    cell.wavedByLabel.text = [NSString stringWithFormat:@"Waved by %@", cell.cellWaveObject.wavedByString]; 

    //round the corners of the image view 
    [self setCornerRadiusForImageView:cell.profilePictureImageView]; 

    //does the wave object have any agrees? 
    if (cell.cellWaveObject.numberOfAgrees > 0) 
    { 
     UIView *agreedView = [[UIView alloc] init]; 

     UILabel *numberOfAgreesLabel = [[UILabel alloc] init]; 
     numberOfAgreesLabel.font = [UIFont boldSystemFontOfSize:13.0f]; 
     numberOfAgreesLabel.textColor = [UIColor whiteColor]; 

     if (cell.cellWaveObject.numberOfAgrees > 1) 
     { 
      numberOfAgreesLabel.text = [NSString stringWithFormat:@"+%i Agree", cell.cellWaveObject.numberOfAgrees]; 
     } 
     else 
     { 
      numberOfAgreesLabel.text = [NSString stringWithFormat:@"+%i Agrees", cell.cellWaveObject.numberOfAgrees]; 

     } 

     UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:AgreedViewImage]]; 

     //get the width of the string 
     CGSize constraintSize = CGSizeMake(242.0f, 16.0f); 
     CGSize stringSize = [numberOfAgreesLabel.text sizeWithFont:numberOfAgreesLabel.font constrainedToSize:constraintSize]; 
     CGFloat agreedViewWidth = stringSize.width + 10.0f; 

     //adjust the frame and add it to the cell 
     agreedView.frame = CGRectMake(310.0f - agreedViewWidth, cell.wavedByLabel.frame.origin.y, agreedViewWidth, 14.0f); 

     backgroundImageView.frame = CGRectMake(5.0f, 0.0f, agreedView.frame.size.width, agreedView.frame.size.height); 
     numberOfAgreesLabel.frame = CGRectMake(5.0f, 0.0f, agreedView.frame.size.width, agreedView.frame.size.height); 
     [agreedView addSubview:backgroundImageView]; 
     [agreedView addSubview:numberOfAgreesLabel]; 

     [cell.contentView addSubview:agreedView]; 
     return cell; 
    } 
    else 
    { 
     return cell; 
    } 
} 
+0

更好地設置在子類的UITableView細胞的類 –

回答

1

可以添加子視圖不刪除從cell.Content視圖。 嘗試使用cellForRow方法中下面的代碼

UIView *agreedView=(UIView*)[cell.contentView viewWithTag:21]; 

//does the wave object have any agrees? 
if (cell.cellWaveObject.numberOfAgrees > 0) 
{ 
    if (!agreedView) { 
     agreedView=[[UIView alloc] init]; 
     agreedView.tag=21; 

     UILabel *numberOfAgreesLabel = [[UILabel alloc] init]; 
     numberOfAgreesLabel.font = [UIFont boldSystemFontOfSize:13.0f]; 
     numberOfAgreesLabel.textColor = [UIColor whiteColor]; 
     numberOfAgreesLabel.tag=22; 

     UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:AgreedViewImage]]; 
     backgroundImageView.tag=23; 

     [agreedView addSubview:backgroundImageView]; 
     [agreedView addSubview:numberOfAgreesLabel]; 

     [cell.contentView addSubview:agreedView]; 
     } 

     numberOfAgreesLabel=(UILabel*)[agreedView viewWithTag:22]; 
     backgroundImageView=(UIImageView*)[agreedView viewWithTag:23]; 

     if (cell.cellWaveObject.numberOfAgrees > 1) 
     { 
      numberOfAgreesLabel.text = [NSString stringWithFormat:@"+%i Agree", cell.cellWaveObject.numberOfAgrees]; 
     } 
     else 
     { 
      numberOfAgreesLabel.text = [NSString stringWithFormat:@"+%i Agrees", cell.cellWaveObject.numberOfAgrees]; 
     } 

     //get the width of the string 
     CGSize constraintSize = CGSizeMake(242.0f, 16.0f); 
     CGSize stringSize = [numberOfAgreesLabel.text sizeWithFont:numberOfAgreesLabel.font constrainedToSize:constraintSize]; 
     CGFloat agreedViewWidth = stringSize.width + 10.0f; 

     //adjust the frame and add it to the cell 
     agreedView.frame = CGRectMake(310.0f - agreedViewWidth, cell.wavedByLabel.frame.origin.y, agreedViewWidth, 14.0f); 

     backgroundImageView.frame = CGRectMake(5.0f, 0.0f, agreedView.frame.size.width, agreedView.frame.size.height); 
     numberOfAgreesLabel.frame = CGRectMake(5.0f, 0.0f, agreedView.frame.size.width, agreedView.frame.size.height); 

    return cell; 
} 
else 
{ 
    if (agreedView) { 
     [agreedView removeFromSuperview]; 
    } 

    return cell; 
} 
+0

感謝您的自定義視圖幀,這有助於! –

0

更好地設置在子類的UITableView細胞的類 聽見你的自定義視圖幀是示例代碼


//.h file of subclassed UITableView cell 

    #import <UIKit/UIKit.h> 

    @interface WavesFeedCell : UITableViewCell 


    @property(nonatomic, retain)UILabel *timeStampLabel; 
    @property(nonatomic, retain)UILabel *waveTextLabel; 
    @property(nonatomic, retain)UILabel *wavedByLabel; 
    @property(nonatomic, retain) id cellWaveObject; //your object from array 

    @end 

    //.m file subclassed UITableView cell 

    #import "WavesFeedCell.h" 

    @implementation WavesFeedCell 

    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
    { 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
    // Initialization code 
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectZero]; 
    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectZero]; 
    UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectZero]; 

    self.timeStampLabel = label1; 
    self.waveTextLabel = label2; 
    self.wavedByLabel = label3; 

    //these are added with zero rect 
    [self addSubview:label1]; 
    [self addSubview:label2]; 
    [self addSubview:label3]; 

    //for agreed imageview 
    UIImageView *agreedImgView = [[UIImageView alloc]initWithFrame:CGRectZero]; 
    agreedImgView.tag = 200; //to access in layoutsubviews. 
    [self addSubview:agreedImgView]; 

    //without ARC 
    [agreedImgView release]; 
    [label1 release]; 
    [label2 release]; 
    [label3 release]; 

    } 
    return self; 
} 

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

// Configure the view for the selected state 
} 

- (void)layoutSubviews 
{ 
    [super layoutSubviews]; 
    //hear set frames for all labels 
    self.timeStampLabel.frame = CGRectMake(5, 5, self.bounds.size.width-30.0f, 40.0f); 
    self.waveTextLabel.frame = CGRectMake(5, 45, self.bounds.size.width-30.0f, 40.0f); 
    self.wavedByLabel.frame = CGRectMake(5, 95, self.bounds.size.width-30.0f, 40.0f); 

    //hear you check weather it is agreed or not 
    if(self.cellWaveObject.numberOfAgrees > 1) 
    { 
     UIImageView *agreedView = (UIImageView *)[self viewWithTag:200]; 
    //set frme for imageview as you did 
    // remember dont add any views hear and set your required frames only because this method called many times 


    } 

//in other class 
//.m file 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 

     WavesFeedCell *cell = [self.aTableView dequeueReusableCellWithIdentifier:@"WavesFeedCell"]; 
    if(cell == nil) 
     { 
      cell = [[WavesFeedCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"WavesFeedCell"]; 
     } 
    //set cell properties 

    return cell; 
    } 


相關問題