ios - DidSelectRowAtIndexPath SWIFT

标签 ios swift uitableview uisearchdisplaycontroller didselectrowatindexpath

我用它创建了一个表和一个搜索栏 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/

相关文章:

ios - 为什么我的 UISwipeGestureRecognizer 出现这个错误?

ios - 如何在 Swift 中退出 "DispatchQueue.main.asyncAfter"

iPhone - 尝试在表格 View 中显示 2 种不同的对象类型

ios - 当 NSOperationQueue 中的所有其他操作完成时执行 NSOperation,无论它们是否成功完成

python - 将 OpenCV Mat 传输到 iPhone 的方法

ios - 缺少 UITableView 部分标题插入

ios - 如果过滤器返回 nil,我如何不返回空结果?

javascript - 移动 safari 上的 iOS html5 视频

ios - GCD - 跟踪总数或异步任务

ios - 调整 View 的高度以包含 TableView 的行数