My ViewController
consists of a UIButton
and a UICollectionView
which has 4 cells. I will select any cell and when I tap the button I want to get the data from only the selected UICollectionViewCell
.
The UIButton
is outside the UICollectionView
and UICollectionViewCell
.
Asked
Active
Viewed 1.6k times
4

Coder
- 400
- 4
- 19

New-Learner
- 229
- 1
- 3
- 15
-
which type of data you need? please spicify all thing and still what you try. – Mitesh Dobareeya May 06 '16 at 10:17
-
@Mitesh: In cell has some text data like name age salary etc. – New-Learner May 06 '16 at 10:20
-
[This](http://stackoverflow.com/a/36699432/3541063) may solve your question. – werediver May 06 '16 at 10:21
-
@werediver This is not very related to my queston. I want data from only selected uicollectionviewcell on uibutton click – New-Learner May 06 '16 at 10:35
-
Then what exactly is your issue? You can't determine selected cell index path? – werediver May 06 '16 at 10:42
-
@werediver My problem is how to get data from selected cell click on UIButton? – New-Learner May 06 '16 at 10:47
-
Is `UIButton` being clicked placed _inside_ the cell or outside of the table? That's important. – werediver May 06 '16 at 10:49
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/111214/discussion-between-new-learner-and-werediver). – New-Learner May 06 '16 at 10:50
-
@werediver Sorry I forgot to mention that........UIButton is outside the UICollectionViewCell and also UICollectionView – New-Learner May 06 '16 at 10:53
1 Answers
22
You can use indexPathsForSelectedItems
to get the indexPaths
for all selected Items. After you requested all the IndexPath
you can simply ask the collectionView for the corresponding cell to get your Data.
import UIKit
class TestCell: UICollectionViewCell {
var data : String?
}
class ViewController: UIViewController {
var model = [["1","2","3","4"]]
@IBOutlet weak var collectionView: UICollectionView?
@IBAction func buttonTapped(sender: AnyObject) {
if let collectionView = self.collectionView,
let indexPath = collectionView.indexPathsForSelectedItems?.first,
let cell = collectionView.cellForItem(at: indexPath) as? TestCell,
let data = cell.data {
print(data)
}
}
}
extension ViewController : UICollectionViewDataSource {
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return model.count
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return model[section].count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("test", forIndexPath: indexPath) as! TestCell
cell.data = self.model[indexPath.section][indexPath.row]
return cell
}
}

Sebastian Boldt
- 5,283
- 9
- 52
- 64
-
-
I am having a similar issue, can you see the link below:http://stackoverflow.com/questions/43701497/cannot-pass-uicollectionview-cell-data-to-different-viewcontroller – tccpg288 Apr 30 '17 at 02:31