gpt4 book ai didi

ios - DidSelectRowAtIndexPath SWIFT

转载 作者:行者123 更新时间:2023-11-30 13:49:42 27 4
gpt4 key购买 nike

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com