2016-09-22 3 views
0

テーブルビューセルがタップされているかどうかを検出する方法を教えてください。 Web上のガイドとチュートリアルは、表のセルビューにプロトタイプのセルを追加する方法に従います。このボタンはすべての行で同じままです。タップすると、行またはセルのインデックスが返されます。xcode 7でテーブルビューセルがタップされているかどうかを検出する方法

ボタンなしでこれを行う必要があります。

@IBAction 

セルがタップされた後、機能内でセグを実行するとどうなりますか。

どうすればよいですか?

私は、次のコードを追加しましたが、あなたはUITableViewDelegateメソッドを使用していないなぜこれが何

class MoreViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 

let moreOption = [ 
    ("My Profile"), 
    ("Settings"), 
    ("Logout") 
] 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 
    self.navigationController!.navigationBar.barTintColor = UIColor(red: (48/255.0), green: (187/255.0), blue: (197/255.0), alpha: 1.0); 
    self.navigationController!.navigationBar.tintColor = UIColor .whiteColor(); 
    self.navigationController!.navigationBar.translucent = false; 
    self.navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor .whiteColor()] 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

func logout() { 
    print("Logout tapped"); 
} 


func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return moreOption.count 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell 
    let (celloption) = moreOption[indexPath.row]; 
    cell.textLabel!.text = celloption; 
    cell.textLabel!.textColor = UIColor(red: (74/255.0), green: (74/255.0), blue: (74/255.0), alpha: 1.0); 
    return cell; 
} 


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    let cell = tableView.cellForRowAtIndexPath(indexPath) 
    print(cell); 
} 

}

+1

のためにそれ

ためapropiateデリゲートとデータソースを設定します。 – Sahil

答えて

2

を印刷しないのですか?

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 

この方法では、タップしたセルのindexPathが表示されます。必要に応じて、ここで新しいコントローラを開くことができます。

+0

私はdidSelectRowAtの内部で次のコードを使用しています - cell = tableView.cellForRowAtIndexPath(indexPath) print(cell); - これは何も印刷されていません –

+0

このメソッドは呼び出されますか? tableViewデータソースを設定してデリゲートしましたか? cellForRowAtIndexPathは、セルの作成に使用されます。 didSelectRowAtIndexPathを使用して、セルタップイベントを取得します。 – PGDev

+0

はい、質問にコードを追加しています –

0

は(あなたがコードからそれを構築している場合または変数)を使用すると、テーブルビューのコンセントを追加さ​​れていないコードにこの

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
      let selectedItem = items.objectAtIndex(indexPath.row) as String 
      print(selectedItem) 
     } 
0

を試してみてください。 あなたのクラスにテーブルビューのコンセントを追加する最初の必要があると、あなたはちょうどdidSelectRow UITableViewDelegateメソッドを使用する必要がある例

//this is connected to your storyboard 
//It may have typing errors it is not tested 
//at this on the top of your class 
@IBOutlet weak var table: UITableView! 

//at this on viewdidLoad or viewDidAppear 
table.delegate = self 
table.datasource = self 
関連する問題