2017-02-07 12 views
0

アクションでtableViewセルを作成しました。選択されているセルのindexPathを知るためのアクションが必要です。私は、アクションにパラメータとしてindexPathを渡す方法や、その周りに他の方法を見つける方法を見つけることができません。ここでのtableView cellForRowAtと実行されるアクションのためのコードは次のとおりです。Swiftのアクション関数にパラメータを渡す

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "menuCell", for: indexPath) as! CustomCell 
    cell.foodName.text = self.menuItems[indexPath.row] 
    //adding gesture recognizer with action Tap to cell 
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(Tap(gesture:index:IndexPath.row))) 
    cell.addGestureRecognizer(tapGesture) 
    return cell 
} 

func Tap(gesture: UIGestureRecognizer , index: Int){ 
    print("Tap") 
    //necessary so that the page does not open twice 
    //adding the rating view 
    let ratingVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "rating") as! RatingView 
    ratingVC.foodName = selectedItem 
    self.addChildViewController(ratingVC) 
    ratingVC.view.frame = self.view.frame 
    self.view.addSubview(ratingVC.view) 
    ratingVC.didMove(toParentViewController: self) 
} 

私はタップにパラメータとしてIndexPath.rowを渡す方法を見つけ出すことはできません。

+0

tableViewで1つの行のみを選択できるようにする場合は、 'fund tap'の' indexPathForSelectedRow'プロパティを使用して、現在選択されている行のindexPathを取得できます。プロパティの詳細については、[Apple Doc](https://developer.apple.com/reference/uikit/uitableview/1615000-indexpathforselectedrow)を参照してください。 –

答えて

0

セルを選択する機能を使用する必要はありません。UITableViewDelegateにはすでにこの種の動作があります。 、

は、指定された行が今そう

を選択されているデリゲートに通知します:UITableViewDelegateに準拠し、tableView(_:didSelectRowAt:)方法を実施することにより

は、あなたが選択したセルのindexPath.rowを取得することができますtableView(_:didSelectRowAt:)を実装した後、tapGestureの機能を削除する必要があります。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "menuCell", for: indexPath) as! CustomCell 
    cell.foodName.text = self.menuItems[indexPath.row] 

    return cell 
} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    print("Selected row: \(indexPath.row)") 
} 
+0

私はクラス内の他の場所にその機能を持っています。問題は、セルがタップされ、そのページの内容がどのセルがタップされているかによって異なるページを開くことです。 –

+0

これは 'tableView(_:didSelectRowAt:)'メソッドの中で実装することができます。私はあなたがindexPath.rowであることに基づいて選択されたセルを認識すると仮定します。 –

+0

これは私が探していた機能です!私はそのことを考えなかったと信じられません。 –

0

didSelectRowAt Indexpathメソッドを使用してください。

カスタムタップの使用手順は次のとおりです。

  1. セルにタグを割り当てます。

    せtapGesture = UITapGestureRecognizer(ターゲット:自己、アクション:#selector(self.tapBlurButton(_ :)))

    cell.tag = indexPath.row 
    cell.addGestureRecognizer(tapGesture) 
    

2.Manageタップイベント

func tapBlurButton(_ sender: UITapGestureRecognizer) { 
    //Get indexpath.row value or Indexpath Value 
print(sender.view.tag) 

} 

・ホープ助けになる。 :)

0

まず、タップジェスチャーを削除します。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
//add your rating view code here 
let ratingVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "rating") as! RatingView 
ratingVC.foodName = self.menuItems[indexPath.row] //get the selected item 
self.addChildViewController(ratingVC) 
ratingVC.view.frame = self.view.frame 
self.view.addSubview(ratingVC.view) 
ratingVC.didMove(toParentViewController: self) 

}もちろん

、あなたはすべてのエラーをキャッチすることを確認するためにチェックし、警備員を追加したくなるでしょう。第二に、このようなtableViewdidSelectRowデリゲートを実装します。

関連する問題