2014-10-01 70 views
0

我有一個表單調用單獨的UITableViewCell。我有代碼來佈局UITableViewCell中的單元格,並在我的tableview中正確顯示。不過,由於某些原因,如果我選擇單元格,單元格中的內容將重繪在單元格的頂部。TableViewCell內容在選中單元格時重複

有誰知道這是爲什麼?我附上了代碼來繪製下面的單元格。

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

if (tableView == productInformationTable){ 

    if (indexPath.row == 0) { 
     static NSString *CellIdentifier = @"Cell"; 
     ProductImagesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      cell = [[ProductImagesTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
     } 
     else{ 
      cell = [[ProductImagesTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
     } 
     return cell; 
    } 

    else if (indexPath.row == 1) { 
     static NSString *CellIdentifier = @"Cell"; 
     ProductQuantityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 

     if (cell == nil) { 
      cell = [[ProductQuantityTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
     } 
     else{ 
      cell = [[ProductQuantityTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
     } 
     return cell; 
    } 

    else if (indexPath.row == 2) { 
     static NSString *CellIdentifier = @"Cell"; 
     ProductButtonsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      cell = [[ProductButtonsTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
     } 
     else{ 
      cell = [[ProductButtonsTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
     } 
     return cell; 
    } 



    else { 
     static NSString *CellIdentifier = @"Cell"; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
     } 
     else{ 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
     } 
     return cell; 
    } 
} 

else { 
    return nil; 
} 

}

我的細胞的代碼如下 -

- (void)awakeFromNib { 

    } 




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

    } 

- (void)layoutSubviews 
{ 
    [super layoutSubviews]; 

    self.contentView.backgroundColor = [UIColor redColor]; 
    customButton = [[SAExpandableButton alloc]initWithFrame:CGRectMake(self.frame.size.width - 50, self.frame.size.height/2 - 15, 30, 30)]; 
    customButton.layer.borderColor = [Styles priceRed].CGColor; 
    customButton.layer.borderWidth = 1.5; 
    customButton.layer.cornerRadius = 15; 
    customButton.expandDirection = SAExpandDirectionLeft; 
    customButton.numberOfButtons = 8; 
    customButton.selectedIndex = 0; 

    quantityLabel = [UILabel new]; 
    quantityLabel.frame = CGRectMake(10, self.frame.size.height/2 - 10, 100, 20); 
    quantityLabel.text = @"Quantity"; 
    [self addSubview:quantityLabel]; 


    customButton.buttonTitles = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8", nil]; 

    [self addSubview:customButton]; 



} 



- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath]; 
if(cell.selectionStyle == UITableViewCellSelectionStyleNone){ 
    return nil; 
} 
return indexPath; 
NSLog(@"willSelectRow called"); 

} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSLog(@"Table Pressed"); 

} 
+0

嗯...不是沒有看到您的代碼。 – rdelmar 2014-10-01 05:14:05

+0

請提供您的相關代碼部分,以便我們可以調查問題。 – fluidsonic 2014-10-01 05:14:13

+0

對不起,我現在就加入它。我知道它必須是因爲layoutSubviews每次都被調用,但我不明白爲什麼。 – crashlog 2014-10-01 05:22:45

回答

1

layoutSubviews你絕不能造成你的電池的子視圖,因爲它可以被調用任意多次。使用initWithStyle:reuseIdentifier:進行初始化,並將它們排列在layoutSubviews(因此名稱)。

此外,您必須爲所有不同的細胞類型使用不同的CellIdentifier

編輯:

爲了防止一個小區的選擇,你應該使用tableView:willSelectRowAtIndexPath:並返回nil防止選擇。

相關問題