我目前遇到了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