2016-09-22 19 views
0

如何檢測是否輕敲表格視圖單元格。網站上的指南和教程遵循向表格單元格視圖添加按鈕的方法 - 原型單元格。對於所有行,此按鈕保持不變,當點擊時,返回行或單元格的索引。如何檢測表格視圖單元格是否在xcode 7中輕敲

我想這樣做沒有按鈕。我怎樣才能調用

@IBAction 

當一個單元被點擊,然後執行一個segue函數。

我應該怎麼辦?

添加以下代碼,但是這並不能顯示任何信息

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

你只需要使用didSelectRow的UITableViewDelegate方法。 – Sahil

回答

2

你爲什麼不使用的UITableViewDelegate方法?

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

在這種方法中,你將得到挖掘電池的indexPath這裏本身,如果你願意,你可以打開新的控制器。

+0

我在didSelectRowAt中使用下面的代碼 - let 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

(如果您是從代碼構建它或變量)你是不是添加的tableview出口的代碼。 您需要先在的tableview出口添加到您的類,然後設置apropiate委託和數據源爲它

例如

//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 
相關問題