Ios 在UICollectionView上最多选择4个图像
如果用户选择的图像超过4个,则显示警报框Ios 在UICollectionView上最多选择4个图像,ios,uicollectionview,Ios,Uicollectionview,如果用户选择的图像超过4个,则显示警报框 -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *secell = [collectionView cellForItemAtIndexPath:indexPath]; if (indexPath.row == 4) {
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *secell = [collectionView cellForItemAtIndexPath:indexPath];
if (indexPath.row == 4) {
UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:@"OK Dailog"
message:@"This is OK dialog"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles: nil];
[alert show];
}
else
{
UIImageView *imgselect = (UIImageView *)[secell viewWithTag:110];
imgselect.image = [UIImage imageNamed:@"selectimg.png"];
}
}
您应该检查所选项目的数量,而不是检查indexPath.row。我可以试试这样的
NSArray *selectedIndexPaths = [collectionView indexPathsForSelectedItems];
if (selectedIndexPaths.count > 4)
{
// Show alert
}
这应该在委托方法集合视图中设置:shouldSelectItemAtIndexPath
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
BOOL shouldSelect = collectionView.indexPathsForSelectedItems.count < 4;
if (!shouldSelect) {
// Show Alert
}
return shouldSelect;
}