如果我选择第一部分的第一个indexPath,则会选择不同部分的所有第一个indexPath,就像图像一样。如何纠正呢?
if ([arraySelectedValues containsObject:arrayStateNames[indexPath.row]]) {
cell.imageCheck.image =[UIImage imageNamed:@"check"];
} else {
cell.imageCheck.image =[UIImage imageNamed:@"uncheck"];
}
答案 0 :(得分:1)
将您的代码更改为以下内容。
if ([arraySelectedValues containsObject:indexPath])
{
cell.imageCheck.image =[UIImage imageNamed:@"check"];
}
else
{
cell.imageCheck.image =[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;
}
您可以根据需要使用上述任何方法。