如何在 swift 中将复选框添加到 uicollectionview
How to add checkbox to uicollectionview in swift
前段时间有人问过这个问题并给出了一些答案,但我并不真正理解其中的任何一个。所以我想知道是否有人可以写一个易于理解的教程来说明如何执行下图所示的操作:
http://i.imgur.com/BzIBOkH.jpg?1
如果有人能确切地分享如何做到这一点,我将非常感激,因为它看起来真的很酷,我很想在我的应用程序中使用类似的东西
Here 是复选框单元格可用的示例项目。 (objective - c)
MyCell.m
// A setter method for checked property
- (void)setChecked:(BOOL)checked {
// Save property value
_checked = checked;
// Update checkbox image
if(checked) {
self.checkBoxImageView.image = [UIImage imageNamed:@"Checked"];
} else {
self.checkBoxImageView.image = [UIImage imageNamed:@"Unchecked"];
}
}
您的 Collection 查看单元格
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
// Deselect cell first
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
// Get selected cell
MYCell* cell = (MYCell*) [collectionView cellForItemAtIndexPath:indexPath];
// Check if set contains selected cell indexPath
if([self.checkedIndexPaths member:indexPath])
{
// User tapped on checked cell
// Remove selected indexPath from set
[self.checkedIndexPaths removeObject:indexPath];
// Uncheck checkbox on cell
cell.checked = NO;
}
else // User tapped on unchecked cell
{
// Add selected indexPath to set
[self.checkedIndexPaths addObject:indexPath];
// Check checkbox on cell
cell.checked = YES;
}
}
前段时间有人问过这个问题并给出了一些答案,但我并不真正理解其中的任何一个。所以我想知道是否有人可以写一个易于理解的教程来说明如何执行下图所示的操作:
http://i.imgur.com/BzIBOkH.jpg?1
如果有人能确切地分享如何做到这一点,我将非常感激,因为它看起来真的很酷,我很想在我的应用程序中使用类似的东西
Here 是复选框单元格可用的示例项目。 (objective - c)
MyCell.m
// A setter method for checked property
- (void)setChecked:(BOOL)checked {
// Save property value
_checked = checked;
// Update checkbox image
if(checked) {
self.checkBoxImageView.image = [UIImage imageNamed:@"Checked"];
} else {
self.checkBoxImageView.image = [UIImage imageNamed:@"Unchecked"];
}
}
您的 Collection 查看单元格
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
// Deselect cell first
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
// Get selected cell
MYCell* cell = (MYCell*) [collectionView cellForItemAtIndexPath:indexPath];
// Check if set contains selected cell indexPath
if([self.checkedIndexPaths member:indexPath])
{
// User tapped on checked cell
// Remove selected indexPath from set
[self.checkedIndexPaths removeObject:indexPath];
// Uncheck checkbox on cell
cell.checked = NO;
}
else // User tapped on unchecked cell
{
// Add selected indexPath to set
[self.checkedIndexPaths addObject:indexPath];
// Check checkbox on cell
cell.checked = YES;
}
}