UICollectionViewReusableView
错误:选择一个标题有时会触发另一个标题
UICollectionView
有2个部分。每个都有一个带有按钮的标题,该按钮可以更改或BOOL
的状态,然后重新加载一个部分。这些用于显示每个单元格上的删除按钮是否隐藏。但是,如果我交替并在触摸一个按钮后触摸另一个按钮,那么它似乎将它们链接起来。之后触摸第二个按钮会触发两个变量进行更改。仅当再次触摸第一个按钮以以某种方式取消链接时,才能解决此问题。我无法弄清楚为什么或如何存在链接。textDeleteActive
imageDeleteActive
BOOL
代码(删除了大多数不相关的代码):
UICollectionReusableView
标题视图和UICollectionViewCell
单元格都有@property (nonatomic, strong) IBOutlet deleteButton
. 他们当然每个人都有一个实际的 UIButton。
myUICollectionViewController:
@property (nonatomic) BOOL textDeleteActive;
@property (nonatomic) BOOL imageDeleteActive;
- (void)TextHeaderDeleteButtonDynamicHandler
{
NSLog(@"texthead");
self.textDeleteActive = !self.textDeleteActive;
[self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:1]];
}
- (void)ImageHeaderDeleteButtonDynamicHandler
{
NSLog(@"imagehead");
self.imageDeleteActive = !self.imageDeleteActive;
[self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
long fRow = [indexPath row];
switch ([indexPath section]) {
case 0:
if (true) {
HCSShortCutTextViewCell *theCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyShortCut" forIndexPath:indexPath];
if (self.imageDeleteActive) {
theCell.deleteButton.hidden = NO;
} else {
theCell.deleteButton.hidden = YES;
}
[theCell.deleteButton addTarget:self action:@selector(ImageCellDeleteButtonDynamicHandler:event:) forControlEvents:UIControlEventTouchUpInside];
return theCell;
}
break;
case 1:
if (true) {
HCSCustomViewCell *theCustCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCustom" forIndexPath:indexPath];
//no image
if (self.textDeleteActive) {
theCustCell.deleteButton.hidden = NO;
} else {
theCustCell.deleteButton.hidden = YES;
}
[theCustCell.deleteButton addTarget:self action:@selector(TextCellDeleteButtonDynamicHandler:event:) forControlEvents:UIControlEventTouchUpInside];
return theCustCell;
}
break;
default:
return nil;
break;
}
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//handled by the storyboard segue
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
if (kind == UICollectionElementKindSectionHeader) {
switch ([indexPath section]) {
case 0:
if (true) {
HCSMyHeaderReusableView *theCell = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
theCell.titleLabel.text = @"Image Shortcuts";
NSLog(@"ima");
[theCell.deleteButton addTarget:self action:@selector(ImageHeaderDeleteButtonDynamicHandler) forControlEvents:UIControlEventTouchUpInside];
return theCell;
}
break;
case 1:
if (true) {
HCSMyHeaderReusableView *theCell = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
theCell.titleLabel.text = @"Text Shortcuts";
NSLog(@"tex");
[theCell.deleteButton addTarget:self action:@selector(TextHeaderDeleteButtonDynamicHandler) forControlEvents:UIControlEventTouchUpInside];
return theCell;
}
break;
default:
return nil;
break;
}
} else
return nil;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//set defaults
self.textDeleteActive = NO;
self.imageDeleteActive = NO;
}
- (void)TextCellDeleteButtonDynamicHandler:(id)sender event:(id)event
{
NSLog(@"textcell");
[self deleteItemAndReloadCollectionView:sender event:event defaultsKey:@"textShortcuts"];
}
- (void)ImageCellDeleteButtonDynamicHandler:(id)sender event:(id)event
{
NSLog(@"imagecell");
[self deleteItemAndReloadCollectionView:sender event:event defaultsKey:@"shortcuts"];
}