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