2012-06-27 31 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