Swift 从节的行中的单元格中检索标记
我有一个带有自定义Swift 从节的行中的单元格中检索标记,swift,tableview,Swift,Tableview,我有一个带有自定义单元格的表视图。我已将按钮添加到此单元格(我已为此单元格创建了UITableViewCell子类) 我的数据分布在3个部分和行中。我希望能够在任何一行按下按钮,并将该行内容传递到下一页。当我没有任何部分的时候,我能够做到这一切。这是我的密码: import UIKit 类ViewController:UIViewController、UITableViewDataSource{ var menu = ["a", "b", "c"] @IBOutlet weak var t
单元格的表视图
。我已将按钮
添加到此单元格
(我已为此单元格创建了UITableViewCell
子类)
我的数据分布在3个部分和行中。我希望能够在任何一行按下按钮,并将该行内容传递到下一页。当我没有任何部分的时候,我能够做到这一切。这是我的密码:
import UIKit
类ViewController:UIViewController、UITableViewDataSource{
var menu = ["a", "b", "c"]
@IBOutlet weak var tblview: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// print(menu[section].count)
return menu.count
}
func numberOfSections(in tableView: UITableView) -> Int {
//print(menu.count)
return menu.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! AddItemClassCell
cell.textLabel?.text = menu[indexPath.row]
cell.addBtn.tag = indexPath.row
cell.addBtn.addTarget(self, action: #selector(BtnClicked), for: .touchUpInside)
return cell
}
@objc private func BtnClicked(sender: UIButton) {
let vc = self.storyboard!.instantiateViewController(withIdentifier: "NextVC") as! NextVC
vc.loadViewIfNeeded()
//vc.lb.text = menu[sender.indexPath.section][sender.indexPath.row]
vc.lb.text = menu[sender.tag]
self.navigationController?.pushViewController(vc, animated: true)
}
}
以下是单元类:
导入UIKit
类AddItemClassCell:UITableViewCell{
@IBOutlet weak var addBtn: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
无法将“[String]”类型的值分配给“String”类型
如果您使用的是单元格上的按钮,则可以使用
import UIKit
struct Menu {
let title: String
let innerMenu: [String]
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var myTV: UITableView!
let menuArr = [
Menu(title: "1", innerMenu: ["a", "b", "c"]),
Menu(title: "2", innerMenu: ["d", "e", "f"]),
Menu(title: "3", innerMenu: ["g", "h", "i"]),
]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
myTV.delegate = self
myTV.dataSource = self
myTV.reloadData()
}
func numberOfSections(in tableView: UITableView) -> Int {
return menuArr.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menuArr[section].innerMenu.count
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let title = UILabel()
title.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 50)
title.text = menuArr[section].title
return title
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : MyCell! = tableView.dequeueReusableCell( withIdentifier: "MyCell") as? MyCell
cell.lbl_Title.text = menuArr[indexPath.section].innerMenu[indexPath.row]
cell.btn_Click.addTarget(self, action: #selector(BtnClicked), for: .touchUpInside)
return cell as MyCell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
@objc func BtnClicked(sender:UIButton!){
let view = sender.superview!
let cell = view.superview as! MyCell
let indexPath = myTV.indexPath(for: cell)
print("\(indexPath?.section) - \(indexPath?.row)")
}
}
menu
array声明在哪里?您好,我添加了我的代码,您能看到吗?好的,让我检查一下,所以我想改为:var menu=[[“a”,“b”,“c”],[“d”,“e”,“f”],[“g”,“h”,“I”]]让headerText=[“1”,“2”,“3”],并且当我有多个节时,能够传递数据,但我得到了这个错误:无法分配“[String]类型的值“输入字符串?”在这行代码中:vc.lb.text=menu[sender.tag]嗨,我试图发布我的代码,但我遇到了一个bug,所以无法正常工作,这就是为什么我很晚才回答lol。嗨@Sh_Khan你能从我的帖子中删除你的负面评论吗?正如我所说,我试图发布一个正确的代码。我真的在为我的问题寻找答案。请删除您的副本和您所指的问题。因为它没有回答我的问题。谢谢