gpt4 book ai didi

ios - 从 viewForHeaderInSection 中的 UIButton 获取数据发送到 add Target 中的方法

转载 作者:行者123 更新时间:2023-11-28 07:15:58 25 4
gpt4 key购买 nike

更新更清楚,抱歉菜鸟。

我设置了一个按钮出现在 TableView 每个部分的标题中,该按钮出现并调用指定的函数,但是我想将部分数据传递给调用的方法。

当我尝试使用 UIButton.tag 或什至将部分标题分配给按钮的 textLabel 标题时,它总是返回 nil(即使部分标题正确显示)。

我需要将数据传递给函数,这样我就知道我在处理什么(我想使用另一个 View Controller 向该特定部分的行添加一条新数据)。

类 ViewController:UIViewController、UITableViewDataSource、UITableViewDelegate {

var sectionsDictionary:NSDictionary = ["Fruits" : ["Apple", "Pear", "Plum","Orange","Kiwi","Banana","Grape"], "Veggies" : ["Celery", "Carrots", "Lettuce","Brocoli","Squash"]]

var sectionTitlesArray = []

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

sectionTitlesArray = Array(sectionsDictionary.allKeys)
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {

return sectionTitlesArray.count
}

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
var headerFrame:CGRect = tableView.frame

var title = UILabel(frame: CGRectMake(10, 10, 100, 30))
title.text = sectionTitlesArray.objectAtIndex(section) as? String

var headBttn:UIButton = UIButton.buttonWithType(UIButtonType.ContactAdd) as UIButton
headBttn.enabled = true
headBttn.titleLabel?.text = title.text

headBttn.addTarget(self, action: "showAddVC:", forControlEvents: UIControlEvents.TouchUpInside)

var headerView:UIView = UIView(frame: CGRectMake(0, 0, headerFrame.size.width, headerFrame.size.height))
headerView.addSubview(title)
headerView.addSubview(headBttn)

return headerView

}

func showAddVC(sender: UIButton!) {

println(sender.titleLabel?.text)

}

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

var headerFloat = CGFloat.abs(20)

return headerFloat

}

最佳答案

噢,知道了,我可以通过使用 indexOfObject 并在部分的索引处传递对象来使用 .tag。

headBttn.tag = sectionTitlesArray.indexOfObject(sectionTitlesArray.objectAtIndex(section))

这为 .tag 提供了按下按钮位置的索引,我稍后可以使用它。哇哦。

关于ios - 从 viewForHeaderInSection 中的 UIButton 获取数据发送到 add Target 中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26207819/

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