0
我爲我的表視圖使用自定義單元格。在我的單元格中,我有imageview &按鈕。我在單行中顯示3個圖像。當我選擇按鈕,我使用複選框圖像&當再次點擊按鈕,它取消選擇複選框圖像。當我選擇第一行中的按鈕滾動tabelview,它也檢查第3或第4行上的按鈕。我認爲這是由於細胞的重用。這裏是我的cellForRowAtIndexPath代碼:單元可重用導致在iOS5中重新獲取數據
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"LibraryElementsCell";
LibraryElementsCell *cell = (LibraryElementsCell *) [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *cellArray=[[NSBundle mainBundle] loadNibNamed:@"LibraryElementsCell" owner:self options:nil];
cell=[cellArray objectAtIndex:0];
[cell.firstElementButton setImage:[UIImage imageNamed:CHECKBOX_UNCHECKED_IMAGE] forState:UIControlStateNormal];
[cell.firstElementButton addTarget:self action:@selector(checkBoxSelectedOnLibraryElement:event:) forControlEvents:UIControlEventTouchUpInside];
[cell.secondElementButton setImage:[UIImage imageNamed:CHECKBOX_UNCHECKED_IMAGE] forState:UIControlStateNormal];
[cell.secondElementButton addTarget:self action:@selector(checkBoxSelectedOnLibraryElement:event:) forControlEvents:UIControlEventTouchUpInside];
[cell.thirdElementButton setImage:[UIImage imageNamed:CHECKBOX_UNCHECKED_IMAGE] forState:UIControlStateNormal];
[cell.thirdElementButton addTarget:self action:@selector(checkBoxSelectedOnLibraryElement:event:) forControlEvents:UIControlEventTouchUpInside];
tableView1.backgroundColor = [UIColor clearColor];
tableView1.separatorStyle = UITableViewCellSeparatorStyleNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSMutableArray *tempArray = [self.categoriesArray objectAtIndex:indexPath.section];
int row = indexPath.row * 3;
if (row <= [tempArray count])
{
LibraryElement *libElement = [tempArray objectAtIndex:row];
cell.firstElementImageView.image = [UIImage imageNamed:libElement.imageName];
}
if ((row + 1) < [tempArray count])
{
LibraryElement *libElement = [tempArray objectAtIndex:row+1];
cell.secondElementImageView.image = [UIImage imageNamed:libElement.imageName];
}
else {
[cell.secondElementImageView setHidden:YES];
[cell.secondElementButton setHidden:YES];
}
if ((row + 2) < [tempArray count])
{
LibraryElement *libElement = [tempArray objectAtIndex:row+2];
cell.thirdElementImageView.image = [UIImage imageNamed:libElement.imageName];
}
else {
[cell.thirdElementImageView setHidden:YES];
[cell.thirdElementButton setHidden:YES];
}
return cell;
}
而且有時隱藏按鈕&的ImageView被meessed起來的邏輯。即使數據可用,它也會隱藏imageview &按鈕。
幫助任何knid高度appreciated.Thanks