单元可重用导致在iOS5中重新重复数据
我正在为我的表视图使用自定义单元格。在我的手机中,我有imageview&按钮。我在一行中显示3个图像。当我选择按钮时,我使用的是复选框图像&当再次点击按钮时,它会取消选择复选框图像。当我选择第一行的按钮并滚动选项卡视图时,它还会检查第三行或第四行的按钮。我认为这是由于细胞的重复使用。以下是我的CellForRowatineXpath代码:单元可重用导致在iOS5中重新重复数据,ios5,uitableview,reuseidentifier,Ios5,Uitableview,Reuseidentifier,我正在为我的表视图使用自定义单元格。在我的手机中,我有imageview&按钮。我在一行中显示3个图像。当我选择按钮时,我使用的是复选框图像&当再次点击按钮时,它会取消选择复选框图像。当我选择第一行的按钮并滚动选项卡视图时,它还会检查第三行或第四行的按钮。我认为这是由于细胞的重复使用。以下是我的CellForRowatineXpath代码: - (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPat
- (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;
}
-(UITableViewCell*)tableView:(UITableView*)tableView1 cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“LibraryElementsCell”;
LibraryElementsCell*单元格=(LibraryElementsCell*)[tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil)
{
NSArray*cellArray=[[NSBundle mainBundle]loadNibNamed:@“LibraryElementsCell”所有者:自选项:nil];
单元格=[X射线对象索引:0];
[cell.firstElementButton setImage:[UIImage ImageName:复选框\u未选中\u图像]用于状态:UIControlStateNormal];
[cell.FirstElement按钮添加目标:自我操作:@selector(复选框SelectedOnLibraryElement:event:)for ControlEvents:UIControlEventTouchUpInside];
[cell.SecondElement按钮设置图像:[UIImage ImageName:复选框\u未选中\u图像]用于状态:UIControlStateNormal];
[cell.SecondElement按钮添加目标:自我操作:@selector(复选框SelectedOnLibraryElement:event:)for ControlEvents:UIControlEventTouchUpInside];
[cell.thirdElementButton setImage:[UIImage ImageName:复选框\u未选中\u图像]用于状态:UIControlStateNormal];
[cell.thirdElementButton添加目标:自我操作:@selector(复选框SelectedOnLibraryElement:event:)for ControlEvents:UIControlEventTouchUpInside];
tableView1.backgroundColor=[UIColor clearColor];
tableView1.separatorStyle=UITableViewCellSeparatorStyleNone;
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
NSMutableArray*tempArray=[self.categoriesArray objectAtIndex:indexPath.section];
int row=indexPath.row*3;
如果(行您每次都需要刷新单元格组件
e、 g.在单元格内创建一个刷新方法,该方法获取数据并绘制单元格组件
refreshCellView:(NSDictionary)数据
并在cellForRowAtIndexPath内调用refreshCellView:获取单元格后