从部分

时间:2018-12-30 14:02:43

标签: swift tableview

enter image description here我有一个tableView和一个自定义cell。我已经为此button添加了cell(我为此单元格创建了UITableViewCell子类)。

我的数据分为3节和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

class 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?'

1 个答案:

答案 0 :(得分:-1)

如果您正在使用单元格上的按钮,则可以使用

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)")
}


}