TableViewCell不应该重用

时间:2015-04-06 12:40:40

标签: ios uitableview

我在我的项目中使用了UITableview,而UITableviewcell也使用了UITableview。问题是细胞正在重复使用。如果它被重复使用,当我选择按钮时,我想要为单个细胞和按钮isSelected =YES更改图像。因此,按钮的所选图像将被重用于每个单元格。

所以,请帮我解决这个问题。

3 个答案:

答案 0 :(得分:0)

您必须查看已选择的单元格,并将标记值设置为所选索引。并重新加载表。

重新加载表格时,检查标志值并设置要为所选单元格设置的图像。它会给你不同的细胞选择。

例如

int selectionFlag = -888;

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
{  
    if(selectionFlag == indexPath.row)
    {
        //set the image you want to set for the cell which is selected.
    }   
    else  
    {
        //the normal image you want to set.
    }  
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath   
{   
    selectionFlag = indexPath.row;
    [tableView realoadData];  
}

答案 1 :(得分:0)

// In indexPathRow before return 
cell button.tag=indexPath.row;

In buttonclick action

//
-(IBAction)BtnAction:(UIControl *)sender
{
    UIButton *button = (UIButton*)sender;
    NSIndexPath *buttonIP = [NSIndexPath indexPathForRow:sender.tag];
    //Type cast it to CustomCell
    CustomCell *cell = (CustomCell*)[table cellForRowAtIndexPath:buttonIP];
    if(button.isSelected==NO)
    {
        cell.Imageview.image=selectedImage;
        button.isSelected=YES;
    }
    else
    {
        cell.Imageview.image=OldImage;
        button.isSelected=NO;
    }

    [table reloaddata];
}

答案 2 :(得分:0)

使用上面的代码,你可以使用这行代码

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath        
{  
    if(firstTime)  
    {  
        [tableView cellForRowAtIndexPath:indexPath].imageView.image = [UIImage imageNamed:@"selected image"];  
        [attatancearry addObject:value];  
    }  
    if(secondTime)  
    {
        [tableView cellForRowAtIndexPath:indexPath].imageView.image = [UIImage imageNamed:@"absent image"];  
        [attatancearry removeObject:value];    
    }
}

并在添加了注意力时将值添加到另一个nsmutablearry,当您再次按下同一个单元格时,必须从之前添加的nsmutablearray中删除该值,然后将图像重置为不存在。但是你不必重新加载整个tableview它只会改变你现在选择的单元格。当您将单元格的值或索引号添加到数组中时,可以使用选择值进一步使用。