2015-06-22 60 views
0

前段時間有人已經問過這個問題,並給出了一些答案,但我並沒有真正理解其中的任何一個。所以,我想知道如果任何人都可以請寫一個容易理解如何做下面的圖片上顯示的東西教程:如何將複選框添加到swift中的uicollectionview中

http://i.imgur.com/BzIBOkH.jpg?1

我會如此感激,如果任何人都可以分享究竟如何,是因爲它看起來非常酷,我很樂意在我的應用程序中使用類似的東西

+1

創建自定義單元格,往其中加按鈕。執行它的條件 –

+0

我創建自定義單元格並添加它裏面的按鈕,但是當我選擇其中一個自動選擇其他單元格時,我需要它用戶可以選擇多個食物時在同一單元格上的複選框上點擊 – ahmedcool

+0

使用按鈕的標籤。 button.tag = indexPath.row,並檢查按鈕的標籤後,你可以做到這一點。 @ahmedcool –

回答

1

Here是複選框單元格提供的示例項目。 (目標 - 三)

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"]; 
    } 
} 

您的收藏查看電池

- (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; 
    } 
} 
+0

確定這是工作,但我不需要用戶點擊單元格,我需要用戶點擊複選框本身 – ahmedcool

+0

@ahmedcool你也可以通過按鈕點擊 –

相關問題