我目前遇到了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中,其他一些单元格不显示按钮...这很正常。但几次滚动后,“我”按钮开始消失....我不知道为什么。
有没有人有想法? 感谢:-)
好吧,我认为这是与那些隐藏的细胞... 什么是“取消隐藏”他们的最佳做法?我从来没有这样做! – tvincent
那么,在你的情况下,做与你隐藏它们完全相反的:if(indexPath.section == someSectionWhereYouWantToShowInfo){[cell.infoButton setHidden:NO]; cell.info = @“你想要显示的一些信息”; }' – fzwo
所以这就是我现在所做的: if([[tableInfoSup objectAtIndex:indexPath.row] isEqualToString:@“”]){cell.infoButton setHidden:YES]; cell.info = nil; } 其他{ [cell.infoButton setHidden:NO]; cell.info = [tableInfoSup objectAtIndex:indexPath.row]; } – tvincent