2016-10-06 81 views
-2

在集合視圖中我選擇了一個單元格,但它被選中了多個單元格。UiCollectionview允許單個選擇特定單元格

- (UICollectionViewCell *)collectionView:(UICollectionView  *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; 
    cell.Selectitems.tag=indexPath.row; 
[cell.Selectitems addTarget:self action:@selector(selectitem:) forControlEvents:UIControlEventTouchUpInside]; 
} 
-(void)selectitem :(UIButton *)sender{ 
if ([sender isSelected]) { 
    [sender setSelected: NO]; 
    cell.check = YES; 
    [cell.Selectitems setBackgroundImage:[UIImage imageNamed:@"product-uncheck.png"] forState:UIControlStateNormal]; 
}else{ 
     [sender setSelected: YES]; 
     cell.check = YES; 
     [cell.Selectitems setBackgroundImage:[UIImage imageNamed:@"product-check.png"] forState:UIControlStateNormal]; 
} 
} 

檢查波紋管我的圖片鏈接 [1]:http://i.stack.imgur.com/WIgDH.png

+0

你能添加一些代碼 –

+0

在選擇信息按鈕,我是選擇了一個單元,但它選擇了下行信也。 –

+0

我瞭解,對於您的代碼,如果([sender isSelected]){[sender setSelected:NO];我們無法找到問題 –

回答

0

當你點擊你是不是在第一小區改變圖像的第二單元。

所以一定要確保,如果你點擊任何細胞改變形象product-check.png和以前的細胞圖像作爲product-uncheck.png

0

做這樣當按鈕被按下

-(void)selectitem :(UIButton *)sender{ 

//Get Index path from Button Tag 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0]; 

// get Cell instance and than do whatever you want with Cell items 
CustomCell *cell = *cell = [self.collectionView cellForItemAtIndexPath:indexPath]; 

if ([cell.Selectitems isSelected]) { 
    [cell.Selectitems setSelected: NO]; 
    cell.check = YES; 
    [cell.Selectitems setBackgroundImage:[UIImage imageNamed:@"product-uncheck.png"] forState:UIControlStateNormal]; 
}else{ 
     [cell.Selectitems setSelected: YES]; 
     cell.check = YES; 
     [cell.Selectitems setBackgroundImage:[UIImage imageNamed:@"product-check.png"] forState:UIControlStateNormal]; 
} 
} 

我希望這將解決您的問題。

+0

CustomCell * cell = * cell = [self.collectionView cellForItemAtIndexPath:indexPath];這裏顯示這個錯誤:從不兼容類型'UICollectionViewCell * _Nullable'指定'CustomCell' –

+1

CustomCell * cell = [self.collectionView cellForItemAtIndexPath:indexPath]; 對不起,錯誤,,,,我已經寫了兩個時間「細胞」... ID這給出錯誤比投給細胞到CustomCell – Dhiru

+0

謝謝你寶貴的時間花在我身上。已經有一個答案。 –

相關問題