2012-05-08 20 views
0

當我在UItablevewcell中添加標籤時,如果它在cellview的contentview中爲零。如果它不是零,我將通過標籤採用該標籤而不分配。它是重用該單元格的正確過程。 但是當我不想要第二行中的標籤時,我必須將其隱藏。如何刪除第二行中的標籤 而不隱藏。我需要它在第一行。只在特定行中刪除contentview對象?

回答

0

例如,您可以在出列並創建它們時使用不同的單元標識符。例如@「帶標籤的單元」和@「無標籤的單元」。

或者您可以通過label.tag = MY_INT_TAG來標記此標籤,並通過UILabel *label = [cell viewWithTag:MY_INT_TAG]搜索它將其從超級視圖中的第二行中刪除。它適用於不想子類UITableViewCell。

if (indexPath.row == 0) { 
    UILabel *label = [[UILabel alloc] init]; 
    label.tag = TAG; 
    [cell.contentView addSubview:label]; 
} else if (indexPath.row == 1) { 
    UILabel *label = [cell.contentView viewWithTag:TAG]; 
    [label removeFromSuperView]; 
} 
0

當你重用它們沒有共同的元素的單元格,最好的做法是重新使用前清除細胞子視圖(所有添加的元素)。

這樣你就可以根據自己的需要,每次添加的元素...

你可以這樣做:

for(UIView *view in cell.contentView.subviews){ 
     [view removeFromSuperview]; 
    } 

,或者如果你想更花哨:

[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; 

當然,如果您只想清除特定行中的一個特定元素,那麼當您將元素添加到元素的contentview時,必須爲元素指定一個唯一標記,然後通過訪問它將其刪除通過它的標記值:

將它添加到單元格:

UIImageView *rightArrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow.png"]]; 
rightArrow.tag = 111; 
rightArrow.frame = CGRectMake(290, 16, 4, 8); 
[cell.contentView addSubview:rightArrow]; 

從視圖中刪除了第2行:

if (indexpath.row == 2) { 
    UIImageView *rightArrow = (UIImageView *)[cell.contentView viewWithTag:111]; 
    if (rightArrow) 
     [rightArrow removeFromSuperView]; 

}