- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个 TableViewController 并显示了一些来自数组的数据。我还必须创建一个 TableViewCellController,因为我需要为每一行设置一个按钮,该按钮必须执行一个操作。
所以,这是我的 TableViewController 的一部分:
struct person {
var name:String
var age:String
var address:String
}
class myTableViewController: UITableViewController {
var people = Array<person>()
override func viewDidLoad() {
super.viewDidLoad()
var x = person(name: "Marco", age: "28", address: "street a")
var y = person(name: "Pippo", age: "90", address: "street b")
people.append(x)
people.append(y)
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> myTableViewCell {
let CellId: String = "Cell"
var cell: myTableViewCell = tableView.dequeueReusableCellWithIdentifier(CellId) as myTableViewCell
cell.textLabel!.text = people[indexPath.row].name
return cell
}
}
表格打印的很好,如下图所示:
现在,每次按下“地址”按钮时,我都需要访问属性地址,您可以在图片中看到。所以我在 TableViewController 类中尝试使用以下代码:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> myTableViewCell {
let CellId: String = "Cell"
var cell: myTableViewCell = tableView.dequeueReusableCellWithIdentifier(CellId) as myTableViewCell
cell.textLabel!.text = people[indexPath.row].name
cell.buttonPlay?.tag = indexPath.row
cell.buttonPlay?.addTarget(cell, action: "playActionX:", forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
并且,我从 TableViewCell 正确接收到标签:
class TableViewCell: UITableViewCell {
func playActionX(sender:UIButton!) {
println(sender.tag)
}
一切顺利。但问题是,我无法在标签内发送字符串,而且我不知道如何从我的 TableViewController 访问“地址”字段。
我可以使用什么解决方案?附言。我试图为 TableViewController 类创建一个实例:
func playActionX(sender:UIButton!) {
println(sender.tag)
let instance:MyTableViewController = MyTableViewController()
println(instance.getPeopleArray())
}
(getPeopleArray 只是返回数组 people)但奇怪的是,我收到的数组是空的,我不明白为什么。
感谢您的支持
最佳答案
您的 getPeopleArray
是空的,因为您正在创建 MyTableViewController 的新实例,而不是访问现有实例。但不要走那条路。相反,只需将按钮的目标/操作更改为 View Controller ,而不是单元格。所以改变:
cell.buttonPlay?.addTarget(cell, action: "playActionX:", forControlEvents: UIControlEvents.TouchUpInside)
到
cell.buttonPlay?.addTarget(self, action: "playActionX:", forControlEvents: UIControlEvents.TouchUpInside)
并将playActionX:
函数移动到 TableView Controller 。从那里您可以将标签用作人员数组的索引。
关于ios - 将数据从 TableViewController 传递到 TableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26509357/
[将数据从一个 TableViewCell 传输到另一个 TableViewCell][1] 我有 CountriesViewController 和 tableView ,其中是不同国家/地区的列表
我的 TableView 里面有 3 个 TableViewCell。 第一个 TableViewCell 有:标签 第二个 TableViewCell 有:TextField 第三个 TableVi
我不确定这是否是一个奇怪的设置,但我有两个自定义 tableView 单元格 - 手机手机 电子邮箱 在 PhoneCell 中,我有两个文本字段,这些文本字段中有一些值。在 EmailCell 中,
所以我有一个 ExpyTableView 类型的 mainTableView ( https://github.com/okhanokbay/ExpyTableView )。一切都很好,我设法实现了它
据我了解,我无法使用 socket 连接将单元格中的按钮连接到我的 TableViewController。 不过,我可以使用操作连接将单元格中的按钮连接到我的 TableView。这是我巨大问题的根
我想在动态 UITableViewCell 中添加一个动态 UICollectionView,当 CollectionView 接收并准备 contentView 时,它会自动更新其高度。有什么好的方
I want different images under different heading //ViewController.swift var categories = ["Good Aft
我有以下表格 View : 当用户点击蓝色的 Pay Cash/Card 按钮时,他们会转到另一个 tableView。 我遇到的问题是;当用户单击按钮时,不会在委托(delegate)方法中捕获 t
我有一个带有自定义单元格的 TableView。单元格中的 subview 之一是 UIButton。当用户单击按钮时,我希望背景发生变化。我得到了所有的工作。但问题是,直到我将受影响的单元格滚动出屏
- (void)viewDidLoad { [super viewDidLoad]; [self.tableView registerNib:[UINib nibWithNibName
我今天花了六个多小时研究并尝试创建动态单元,但没有成功。我也找到了一些很棒的教程。我做错了什么,希望有人能告诉我什么。 我的应用: table 开始是空的。单击添加按钮会显示一个模态 VC,其中有两个
请注意此代码。 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITa
我有一个 tableViewCell,总共包含 8 个图像,分为两个 block (每个 block 4 个图像)。这些图像会异步下载并存储到数组中,然后在 tableViewCell 的 cellF
运行我的弹出窗口时,代码显示在左上角,而不是显示在单击图标的同一位置,是否可以更改某些内容来纠正位置? 最佳答案 这是因为您已将 aView 的 CGRect 设置为 x 和 y 为 0。 如果您以编
我用 CustomTableViewCells 制作了一个应用程序。我在 MainStoryBoard 上设置了一个 UIButton。 我想在点击时更改按钮的图像。当我再次点击时,我希望按钮返回到原
我正在编写一个 iOS 应用程序。用户输入电影搜索关键字,它会从 omdbapi.com 获取数据并在表格 View 中显示电影。问题是该 TableView 单元格未正确显示。 项目网址: proj
我目前有一个表格 View ,其中加载的单元格中有一个 uicollectionview。我目前遇到了表格 View 单元格的两个问题。 1) TableView 中的第一个单元格应该具有紫色背景,但
我有一个 TableView,可以在其中加载自定义单元格。在自定义单元格中,有一个文本字段,当表格 View 填充自身时,该文本字段会加载自定义拨号盘并自行关闭。这个 float 数字键盘在初始化期间
这是我的主屏幕当单击单元格突出显示时,描述将转到下一个 View Controller ,并且单元格会突出显示,当我单击未突出显示的单元格时,因此单元格未突出显示,这意味着只有零索引单元格突出显示,没
我想以编程方式将两个文本字段添加到 tableViewCell 子类(稍后会详细介绍),但很难从 View Controller 访问文本字段。 我的 TableView 子类是: - (id)ini
我是一名优秀的程序员,十分优秀!