2017-03-27 3 views
2

나는 안드로이드와 이전 경험을 가지고에서 클릭 셀의 중포 기지 데이터베이스 키를 가져올 수 없습니다 및 iOS의 방법 중 하나를 번역 기대했다 :이 방법에서는이 UICollectionView

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); 
       } 
       }); 

을, 나는 중포 기지 키에 액세스 할 수 있어요 를 클릭하여 다음 활동으로 전달하십시오. iOS (Swift)에서는 키를 가져 와서 새로운 ViewController로 전달하려고합니다. UICollectionView 대리자 메서드와 관련이 있다는 것을 알고 있기 때문에 다소 혼란 스럽습니다.

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) 
    } 
} 

편집 : 여기 내 코드입니다 FirebaseUI 포럼에 게시 후, 나는 다음 코드 줄에 레이어가 필요하다고 판단 할 수 있었다, 어떻게 내 시나리오에 적용 할?

- (FIRDataSnapshot *)snapshotAtIndex:(NSInteger)index; 

답변

1

didSelectItemAt 대리자 메서드에서 다음을 구현합니다.

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    let selectedSnapshot = self.dataSource.items[indexPath.row])) 
    print(selectedSnapshot.key) 
} 

그러면 선택한 셀 인덱스에서 FirebaseUI 데이터 소스의 스냅을 꺼냅니다.