2012-06-27 89 views
1

我目前遇到了UITableView問題,更確切地說是使用了自定義單元格。元素在滾動時從UITableViewCell中消失

我得到了一個表視圖控制器,讓我們說ControllerA,它負責顯示不同的TableViewCells。這些單元格是定義在另一個類中的單元格,比如說ControllerCell。每個單元格包含這些信息按鈕之一(小,圓與「我」)。這些按鈕僅在需要顯示時顯示。

在ControllerCell,我定義什麼是未來:

@property (nonatomic, retain) IBOutlet UIButton *infoButton; 
@property (nonatomic, retain) IBOutlet UIAlertView *alert; 
@property (nonatomic, retain) IBOutlet NSString *info; 
- (IBAction)infoSupButton:(id)sender; 

還有@synthesis,就像每個人都這樣做。然後,我定義這是怎麼回事與警報發生:

- (IBAction)infoSupButton:(id)sender { 
     alert = [[UIAlertView alloc] initWithTitle:@"Informations supplémentaires" 
                message:info 
                delegate:self 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 
     if (info != nil) { 
     [alert show]; 
     } 
} 

與「initWithStyle」一節中,我做

[_infoButton addTarget:self action:@selector(infoSupButton:) forControlEvents:UIControlEventTouchUpInside]; 

這對細胞的聲明。

現在我們來關注ControllerA。

我正在解析一個XML文件以獲取「info」數據,當點擊「infoButton」時應該顯示這些數據。這不是一個真正的問題,因爲我可以獲取這些數據並在控制檯中顯示它。

一旦數據被解析,我填充的NSMutableArray,在viewDidLoad中部分:

tableInfoSup = [[NSMutableArray alloc] init]; 

然後,按照經典方法:

-numberOfSectionsInTableView:(3個部分) -numberOfRowsInSection:(8行第0,4節中1和4中第2部分) -cellForRowAtIndexPath:

我得到3個不同的部分,顯示與不同的信息單元,而在cellForRowAtI ndex方法,我做的:現在

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

static NSString *cellIdentifier = @"ExerciceTableCell"; 
ControllerCell *cell = (ControllerCell *)[tableView dequeueReusableCellWithIdentifier:exerciceTableIdentifier]; 

if (cell == nil) 
{ 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ExerciceTableCell" owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 

} 


if (indexPath.section == 0) { 
    [...] 


    if ([[tableInfoSup objectAtIndex:indexPath.row] isEqualToString:@""]) { 
     [cell.infoButton setHidden:YES]; 
     cell.info = nil; 
    } 
    else { 
     cell.info = [tableInfoSup objectAtIndex:indexPath.row]; 
    } 

} 
if (indexPath.section == 1) { 
    [...] 

    if ([[tableInfoSup objectAtIndex:indexPath.row+8] isEqualToString:@""]) { 
     [cell.infoButton setHidden:YES]; 
     cell.info = nil; 
    } 
    else { 
     cell.info = [tableInfoSup objectAtIndex:indexPath.row+8]; //got the 8 rows from section 0; 
    } 

} 
if (indexPath.section == 2) { 

    [...] 

    if ([[tableInfoSup objectAtIndex:indexPath.row+12] isEqualToString:@""]) { 
     [cell.infoButton setHidden:YES]; 
     cell.info = nil; 
    } 
    else { 
     cell.info = [tableInfoSup objectAtIndex:indexPath.row+12]; //got the 8 rows from section 0, + 4 rows from section 1 
    }  
} 


return cell; 
} 

,問題是,當顯示爲第1次在屏幕上,一切都在奧得河:我得到的細胞與小「i」按鈕,顯示良好的UIAlertView中,其他一些單元格不顯示按鈕...這很正常。但幾次滾動後,「我」按鈕開始消失....我不知道爲什麼。

有沒有人有想法? 感謝:-)

回答

3

在您的tableView:cellForRowAtIndexPath:中,當您不希望顯示信息時隱藏信息,但您不顯示取消隱藏應顯示信息的單元格。

查看該方法的前兩行:正確 - 做的是重用您的單元格,因此當單元格滾動出視圖時,它們將從UITableView中移除並放入重用隊列中。然後,當單元格變得可見時,TableView將從該隊列中獲取單元格 - 如果沒有可用單元格,則創建新單元格。

這一切都很順利,但過了一段時間,具有隱藏信息按鈕的單元放在隊列中。然後,過了一段時間,這些單元格被重用 - 有時用於應該有可見信息的行。

有兩種解決方案:您可以顯式取消隱藏希望顯示的行的信息,也可以使用兩種不同類型的單元格,一種是隱藏信息,另一種是可見信息。然後,您爲每個單元格分配不同的標識符,並根據單元格所在的行,在出列/創建單元格之前設置標識符。

+1

好吧,我認爲這是與那些隱藏的細胞... 什麼是「取消隱藏」他們的最佳做法?我從來沒有這樣做! – tvincent

+2

那麼,在你的情況下,做與你隱藏它們完全相反的:if(indexPath.section == someSectionWhereYouWantToShowInfo){[cell.infoButton setHidden:NO]; cell.info = @「你想要顯示的一些信息」; }' – fzwo

+0

所以這就是我現在所做的: if([[tableInfoSup objectAtIndex:indexPath.row] isEqualToString:@「」]){cell.infoButton setHidden:YES]; cell.info = nil; } 其他{ [cell.infoButton setHidden:NO]; cell.info = [tableInfoSup objectAtIndex:indexPath.row]; } – tvincent