我用它创建了一个表和一个搜索栏 Controller 。我使用了一个结构来创建表内容。我只想将所选单元格中的数据存储在变量中。我是一名新的 iOS 开发人员。请帮忙:) TIA
//my struct
import Foundation
struct areas
{
let name : String
}
// table contents
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
var place :areas
if tableView == self.searchDisplayController!.searchResultsTableView{
place = filteredareas[indexPath.row]
}
else{
place = places[indexPath.row]
}
cell.textLabel!.text = place.name
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
return cell
}
如何使用didSelectRowAtIndexPath知道单击的单元格的内容?
最佳答案
当您单击 tableviewcell 时,将调用 tableviewdelegate 方法 didSelectRowAtIndexPath,该方法为您提供 indexPath,以便您可以访问数组中的行,如下所示:
var place: areas
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
place = filteredareas[indexPath.row]
}
因此,每次您单击表格 View 单元格时,都会调用上述委托(delegate)方法,并且将根据indexPath.row存储适当的变量
关于ios - DidSelectRowAtIndexPath SWIFT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34440802/