首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DidSelectRowAtIndexPath SWIFT

DidSelectRowAtIndexPath SWIFT
EN

Stack Overflow用户
提问于 2015-12-23 17:25:22
回答 2查看 1.4K关注 0票数 0

我用它创建了一个表和一个搜索条控制器。我使用了一个结构来创建表内容。我只想将数据存储在变量中选定的单元格中。我是一个新的iOS developer.please帮助:)

代码语言:javascript
复制
//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来知道点击的单元格的内容?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-23 17:53:11

试试看;

代码语言:javascript
复制
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        var place: areas
        place = filteredareas[indexPath.row]
        // do with place whatever you want
}
票数 -1
EN

Stack Overflow用户

发布于 2015-12-23 19:35:12

当您单击一个tableviewcell -调用tableview委托方法didSelectRowAtIndexPath时,该方法将为您提供indexPath,以便您可以按如下方式访问数组中的行:

代码语言:javascript
复制
     var place: areas


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

因此,每次单击tableview单元格时,都会调用上述委托方法&根据indexPath.row存储适当的变量。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34440802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档