【问题标题】:DidSelectRowAtIndexPath SWIFTDidSelectRowAtIndexPath SWIFT
【发布时间】:2015-12-23 17:25:22
【问题描述】:

我已经用它创建了一个表格和一个搜索栏控制器。我使用了一个结构来创建表格内容。我只想将所选单元格中的数据存储在变量中。我是一个新的 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 知道被点击单元格的内容?

【问题讨论】:

标签: ios swift uitableview uisearchdisplaycontroller didselectrowatindexpath


【解决方案1】:

当您单击 tableviewcell 时,会调用 tableviewdelegate 方法 didSelectRowAtIndexPath,该方法为您提供 indexPath,以便您可以访问数组中的行,如下所示:

     var place: areas


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    place = filteredareas[indexPath.row]
}

所以每次点击 tableview 单元格时,都会调用上述委托方法,并根据 indexPath.row 将适当的变量存储在适当的位置

【讨论】:

    【解决方案2】:

    试试看;

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
            var place: areas
            place = filteredareas[indexPath.row]
            // do with place whatever you want
    }
    

    【讨论】:

    • 请详细说明这段代码是如何回答这个问题的。提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 1970-01-01
    • 2015-10-22
    相关资源
    最近更新 更多