2016-04-26 17 views
0

我用ADLively TableView實現了TableView。TableViewCell堆積並出現一次又一次

但是,如果我滾動的tableView,細胞的文本成爲一次又一次地堆放....

使用「cell.textLabel」好,添加的UILabel並不比好,但如果我用「 cell.textLabel「,我無法調整textLabel的寬度。

(我想補充的UIImageView文本的左側和右側)

所以現在,我使用的方式來增加的UILabel。

我該怎麼辦?

這是代碼。

- (void)viewDidLoad { 
CGRect rect = CGRectMake(0, 50, 320, self.view.frame.size.height-150); 

    self.tableView = [[ADLivelyTableView alloc]initWithFrame:rect style:UITableViewStylePlain]; 
    self.tableView = (ADLivelyTableView *)self.tableView; 
    self.tableView.initialCellTransformBlock = ADLivelyTransformFan; 

    self.tableView.delegate = self; 
    self.tableView.dataSource = self; 
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 
    [self.view addSubview: self.tableView]; 
    //self.tableView.backgroundColor = [UIColor blueColor]; 
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [self.section count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
    [self updateCell:cell atIndexPath:indexPath]; 

    if (cell == nil){ 
     myCellView = [[UITableViewCell alloc] 
         initWithStyle:UITableViewCellStyleDefault 
         reuseIdentifier:@"Cell"]; 

     NSMutableArray *set = [self.section objectAtIndex:indexPath.row]; 

     tableTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 7, 220, 30)]; 
     tableTitleLabel.text = [set objectAtIndex:0]; 
     [tableTitleLabel setNumberOfLines:0]; 
     tableTitleLabel.backgroundColor = [UIColor clearColor]; 
     tableTitleLabel.textColor = [UIColor blackColor]; 
     tableTitleLabel.font = [UIFont fontWithName:@"HiraKakuProN-W3" size:15]; 
     tableTitleLabel.adjustsFontSizeToFitWidth = YES; 
     tableTitleLabel.minimumScaleFactor = 10.0f; 
     [myCellView.contentView addSubview:tableTitleLabel]; 

     } 

    NSMutableArray *set = [self.section objectAtIndex:indexPath.row]; 
    [(UILabel *)[cell.contentView viewWithTag:1] setText:[set objectAtIndex:0]]; 

     return cell; 
} 

- (void)updateCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{ 


NSMutableArray *set = [self.section objectAtIndex:indexPath.row]; 

      tableTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 7, 220, 30)]; 
      tableTitleLabel.text = [set objectAtIndex:0]; 
      [tableTitleLabel setNumberOfLines:0]; 
      tableTitleLabel.backgroundColor = [UIColor clearColor]; 
      tableTitleLabel.textColor = [UIColor blackColor]; 
      tableTitleLabel.font = [UIFont fontWithName:@"HiraKakuProN-W3" size:15]; 
      tableTitleLabel.adjustsFontSizeToFitWidth = YES; 
      tableTitleLabel.minimumScaleFactor = 10.0f; 
      [cell.contentView addSubview:tableTitleLabel]; 
} 
+2

請勿將子視圖添加到單元格。它們被重用...使用自定義的UITableViewCell。 – Larme

回答

1

這不是一個好主意,不斷滾動增加細胞子視圖,但如果你不想改變你已經寫的代碼,請使用以下打電話之前刪除該小區的所有子視圖你的方法是updateCell

for (UIView *view in [cell subviews]) 
{ 
    [view removeFromSuperview]; 
} 
+0

謝謝你的回答。如果我添加你的代碼,標籤不顯示... – bao

+0

如果這個答案是正確的,請接受它。謝謝 –