在UItableview中具有多个部分的多个选择

时间:2016-08-17 06:34:07

标签: ios objective-c uitableview

如果我选择第一部分的第一个indexPath,则会选择不同部分的所有第一个indexPath,就像图像一样。如何纠正呢?

enter image description here

if ([arraySelectedValue‌​s containsObject:array‌​StateNames[indexPath.‌​row]]) { 
    cell.imageCheck.imag‌​e =[UIImage imageNamed:@"check"]‌​; 
} else { 
    cell.imageCheck.imag‌​e =[UIImage imageNamed:@"uncheck‌​"];
}

2 个答案:

答案 0 :(得分:1)

将您的代码更改为以下内容。

if ([arraySelectedValue‌​s containsObject:indexPath])
{
    cell.imageCheck.imag‌​e =[UIImage imageNamed:@"check"]‌​;
}
else
{
    cell.imageCheck.imag‌​e =[UIImage imageNamed:@"uncheck‌​"];

}

并在didSelectRow方法中编写以下代码

if ([arraySelectedValues containsObject:indexPath])
{
    [arraySelectedValues removeObject:indexPath];
}
else
{
    [arraySelectedValues addObject:indexPath];

}

并重新加载部分或行

答案 1 :(得分:0)

为此,您需要创建一个全局arr来保存h文件中的选定indexPath

NSMutable *arrSelectedIndex;
你的didSelectItemAtIndexPath中的

你需要写下面的代码。

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self selectDeselectIndexpath:indexPath];
    [collectionView reloadData];

}


// this method is used to toggle between selection and deselection 
-(void) selectDeselectIndexpath : (NSIndexPath *) indexPath{

    for (int i=0; i < arrSelectedIndex.count; i++) {
        NSIndexPath *index = [arrSelectedIndex objectAtIndex:i];
        if (index.row == indexPath.row && index.section == indexPath.section) {
            [arrSelectedIndex removeObject:index];
            return ;
        }
    }
    [arrSelectedIndex addObject:indexPath];

}
// this method is used to select any indexPath
-(void) selectIndexpath : (NSIndexPath *) indexPath{

    for (int i=0; i < arrSelectedIndex.count; i++) {
        NSIndexPath *index = [arrSelectedIndex objectAtIndex:i];
        if (index.row == indexPath.row && index.section == indexPath.section) {
            return ; // This means indexPath already selected
        }
    }
    [arrSelectedIndex addObject:indexPath];
}
    // this method is used to deselect any indexPath
-(void) deselectIndexpath : (NSIndexPath *) indexPath{

    for (int i=0; i < arrSelectedIndex.count; i++) {
        NSIndexPath *index = [arrSelectedIndex objectAtIndex:i];
        if (index.row == indexPath.row && index.section == indexPath.section) {
            [arrSelectedIndex removeObject:index];
            return ; // Indexpath found and remove from array
        }
    }
    // Reaching over here means this indexpath not selected;
}

您可以根据需要使用上述任何方法。