我用它创建了一个表和一个搜索条控制器。我使用了一个结构来创建表内容。我只想将数据存储在变量中选定的单元格中。我是一个新的iOS developer.please帮助:)
//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来知道点击的单元格的内容?
发布于 2015-12-23 17:53:11
试试看;
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var place: areas
place = filteredareas[indexPath.row]
// do with place whatever you want
}发布于 2015-12-23 19:35:12
当您单击一个tableviewcell -调用tableview委托方法didSelectRowAtIndexPath时,该方法将为您提供indexPath,以便您可以按如下方式访问数组中的行:
var place: areas
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
place = filteredareas[indexPath.row]
}因此,每次单击tableview单元格时,都会调用上述委托方法&根据indexPath.row存储适当的变量。
https://stackoverflow.com/questions/34440802
复制相似问题