繁体   English   中英

如何在UITableView按钮中单击以获取特定的行按钮标题

[英]How to get particular row button title in UITableView button click in swift

创建学校时间表,使用UITableView实施设计 在此处输入图片说明

2天6个周期,周期是动态的,它可以显示n个数字。

我的代码:

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dayArr.count-1
    }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell:PeriodCell = tableView.dequeueReusableCell(withIdentifier: "PeriodCell") as! PeriodCell
    cell.selectionStyle = .none
    tableView.separatorStyle = .none

    var x: Int = 2
    for i in 0 ..< periodArr.count
    {
            let btn: UIButton = UIButton(frame: CGRect(x: x, y: 0, width: 110, height: 45))
            btn.backgroundColor = UIColor.clear
            btn.setTitle("Select", for: .normal)
            btn.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
            btn.tag = indexPath.row
            btn.layer.cornerRadius = 5
            btn.layer.borderWidth = 1
            btn.layer.borderColor = UIColor.lightGray.cgColor
            btn.setTitleColor(UIColor.black, for: .normal)
            cell.contentView.addSubview(btn)
            x = x + 115
    }

    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 50
}

我的按钮点击操作:

@objc func buttonAction(sender: UIButton!) {
        let btnsendtag: UIButton = sender
        print("btnsendtag",btnsendtag.tag)
        sender.setTitle("Tamil",for: .normal)
        for i in 0 ..< periodArr.count
        {
            print("btnsendtag",btnsendtag.tag)
            if let buttonTitle = btnsendtag.title(for: .normal)
            {
                print("buttonTitle",buttonTitle)
            }
        }
    }

我的问题:1.我想获取每个按钮的标题。 星期一和星期二以数组形式分开,并分别发送到webservices [或]获取所有按钮标题,并以逗号分隔的形式发送给webservices。[期间是动态的,它可以显示n个数字]想获得所有标题。 每天仅显示6天。

  1. 如何识别选择了哪个按钮,正在添加哪个按钮值以及没有选择按钮的情况下,希望将空标题发送到Webservies。 如何识别。

如何编写按钮单击操作的代码。 请帮助我,我一周以来都在为此苦苦挣扎。 提前致谢。

您可以使用标题项目概念,以将其轻松归档在集合视图中,而对于表视图,则很难获得您想要的这种类型的东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM