Reputation: 3352
I have previous experience with Android and was hoping to translate one of the methods to iOS:
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.v("TAG", model.toString());
Log.v("TAG","ONCLICKED");
Intent toPoll = new Intent(getActivity(), PollHostActivity.class);
//TODO: Go back to clicked item when navigating from Poll Fragment back to LiveFragment
toPoll.putExtra("POLL_ID", mFireAdapter.getRef(viewHolder.getAdapterPosition()).getKey());
startActivity(toPoll);
}
});
In this method, I am able to access the Firebase key of the clicked item and pass it to the next activity. In iOS (Swift) I want to get the key and pass to a new ViewController as well. It is a bit confusing to me, as I know that it has to do with a UICollectionView delegate method. Here is my code:
import UIKit
import FirebaseDatabase
import FirebaseDatabaseUI
import FirebaseStorageUI
import Material
private let reuseIdentifier = "PollCell"
class TrendingViewController: UICollectionViewController {
var ref: FIRDatabaseReference!
var dataSource: FUICollectionViewDataSource!
fileprivate var menuButton: IconButton!
override func viewDidLoad() {
super.viewDidLoad()
ref = FIRDatabase.database().reference()
prepareMenuButton()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Register cell classes
self.dataSource = self.collectionView?.bind(to: self.ref.child("Polls")) { collectionView, indexPath, snap in
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PollCell
/* populate cell */
cell.pollQuestion.text = snap.childSnapshot(forPath: "question").value as! String?
let urlPollImage = snap.childSnapshot(forPath: "image_URL").value as! String?
cell.imageView.sd_setImage(with: URL(string: urlPollImage!), placeholderImage: UIImage(named: "Fan_Polls_Logo.png"))
//Comment
return cell
}
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print(indexPath)
}
extension TrendingViewController {
fileprivate func prepareMenuButton() {
menuButton = IconButton(image: Icon.cm.menu)
}
}
EDIT: After posting on the FirebaseUI forum, I was able to determine that I need to layer in the following line of code, how would I apply it to my scenario?:
- (FIRDataSnapshot *)snapshotAtIndex:(NSInteger)index;
Upvotes: 2
Views: 81
Reputation: 1750
Implement the following in your didSelectItemAt
delegate method.
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let selectedSnapshot = self.dataSource.items[indexPath.row]))
print(selectedSnapshot.key)
}
That pulls the snap out of your FirebaseUI dataSource at the selected cell index.
Upvotes: 2