![](/img/trans.png)
[英]How to get indexPath.row of tableView which is currently being displayed?
[英]How to know which IndexPath.row is clicked on IBAction, and how to exclude a specific button from it being triggered
我有一個 UITableViewController。 它有一個原型單元格,並根據用戶查詢生成 0 到 100 個單元格。 加載后,如果用戶點擊單元格內的任何位置(特定按鈕除外),我希望 IBAction 觸發。 我有多個標簽,如果用戶點擊它們,我仍然希望觸發 IBAction。 我該如何實現?
這是我加載表格的代碼:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! TypeOfActivityTableViewCell
cell.activityLabel?.text = activityNames[indexPath.row]
cell.bgImage?.image = UIImage(named: activityPic[indexPath.row])
cell.sloganLabel?.text = slogan[indexPath.row]
return cell
}
我不想在 tableView 中為 MVC 原則編寫所有這些代碼。
要點擊單元格中的任意位置,您無需配置 IBAction。 只需使用默認的 UITableViewDelegate 即:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
let geoSearchWord = "geoSearchWord" + searchQuery[indexPath.row]
let geoSearchLat = "&geoSearchWordLat=" + (lat == "" ? "33.9700" : lat)
let geoSearchLon = "&geoSearchWordLon=" + (lat == "" ? "-118.4180" : lon)
let geoSearchRadius = "&geoSearchWordRad=5mi"
let twitterURLRequest: String = "https://quiet-cove-5048.herokuapp.com/tw?\(geoSearchWord)\(geoSearchLat)\ (geoSearchLon)\(geoSearchRadius)"
alamoRequest(twitterURLRequest)
}
否則,如果您的單元格中有用於特定操作的按鈕。 您必須向該按鈕添加操作並分配一個標簽以進行識別(點擊哪個按鈕)。
您可以在 cellForRowAtIndexPath 中添加操作
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! TypeOfActivityTableViewCell
cell.activityLabel?.text = activityNames[indexPath.row]
cell.bgImage?.image = UIImage(named: activityPic[indexPath.row])
cell.sloganLabel?.text = slogan[indexPath.row]
cell.myButton.addTarget(self, action: "myAction:", forControlEvents: .TouchUpInside)
cell.myButton.tag = indexPath.row
return cell
}
點擊單元格內的按鈕將執行以下功能:
func myAction(sender:UIButton){
let selectedActivityName = activityNames[sender.tag]
}
如果要單擊單元格中的任何位置,則可以使用其委托方法
IE
tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
如果您想添加UIControl
和該控件的觸摸事件,您需要執行以下操作:
首先,您需要在cellForRowAtIndexPath
為您的控件添加目標,如下所示。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! TypeOfActivityTableViewCell
cell.activityLabel?.text = activityNames[indexPath.row]
cell.bgImage?.image = UIImage(named: activityPic[indexPath.row])
cell.sloganLabel?.text = slogan[indexPath.row]
cell.btn.addTarget(self, action: "btnClickAction", forControlEvents: UIControlEvents.TouchDragInside)
return cell
}
按鈕點擊功能:
func btnClickAction(sender: UIButton)
{
var superView = sender.superview
while superView?.isKindOfClass(UITableViewCell) == false
{
superView = superView?.superview
}
var cell = superView as! UITableViewCell
var indexPath = self.tableView.indexPathForCell(cell)! as NSIndexPath
}
在上面的代碼中,您將找到indexPath
。
這是一個巧妙的解決方案。您可以添加一個 UIControl(例如 UIButton)來接收該特殊區域的觸摸。然后在UITableViewDelegate: tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
接收事件UITableViewDelegate: tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.