繁体   English   中英

如何在swift4单元格数据中选中Table视图单元格来自服务器

[英]How to Checkmark the Table view cells in swift4 cell data is coming from server

正如我在某些情况下尝试的那样,但如果选择第二个单元格,则不能完全正常工作,则第一个单元格中的复选标记将处于取消选中状态,有时直到单击10至20次后,该功能才完全无法使用。这是我的代码。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if tableView == switchTableView{
       return self.arrdata20.count
    } else
    {
        return self.arrdata.count
    }

    }


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

    if (tableView == self.switchTableView)
    {

        let cell:switchTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell1") as! switchTableViewCell

        cell.nameLbl.text = (arrdata20[indexPath.row].name)
        print(cell.nameLbl.text)
        if (arrdata20[indexPath.row].emp_id == "001")
        {
            cell.isHidden=true
        }
        else{
            cell.isHidden=false
        }
        return cell



    }
    else  {
        let cell:PartyTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! PartyTableViewCell
        cell.venuLbl.text = "Venu: \(arrdata[indexPath.row].place)"
        cell.dateTimeLbl.text = "Date & Time: \(arrdata[indexPath.row].date)"
        cell.reasonLbl.text = "Reason: \(arrdata[indexPath.row].reason)"
        //        cell.timeLbl.text = ""
        return cell
    }

}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    var rowHeight:CGFloat = 0.0
    if tableView == self.switchTableView{
    if(arrdata20[indexPath.row].emp_id == "001")
    {
        rowHeight = 0.0
    }
    else
    {
        rowHeight = UITableViewAutomaticDimension  
    }

    return rowHeight
    }else{
        return UITableViewAutomaticDimension
    }

}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


    id1 = "\(arrdata[indexPath.row].id)"
    print(id1)

        if self.switchTableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCellAccessoryType.checkmark
        {
           self.switchTableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none

        }
        else{
            self.switchTableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark



    }
}

选择tableView单元格后,我需要获取选中标记单元格的详细信息,例如单元格中的名称,如下图所示 在此处输入图片说明

您必须将数组声明为:

var checked = [Bool]()

然后在API调用中添加此行代码,以接收arraydata中的数据

self.checked = Array(repeating: false, count: self.arraydata.count)

在表格视图委托方法中:

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

    let cell:switchTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell1") as! switchTableViewCell

    //configure you cell here.
    if checked[indexPath.row] == false{
        cell.accessoryType = .none


    } else if checked[indexPath.row] {
        cell.accessoryType = .checkmark


    }
    cell.title.text = self. arraydata[indexPath.row]["amp_id"].string
    return cell

}

添加另一个委托方法:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    tableView.deselectRow(at: indexPath, animated: true)
    if let cell = tableView.cellForRow(at: indexPath as IndexPath) {

        if cell.accessoryType == .checkmark {
            cell.accessoryType = .none
            checked[indexPath.row] = false


        } else  {
            cell.accessoryType = .checkmark
            checked[indexPath.row] = true


        }
    }

    on OKClickedButtonAction:

    serviceString = ""
    for i in 0..<checked.count{
        if checked[i] == true{
            serviceString = serviceString + self.arraydata[i]["emp_id"].string! + ", "
            print(serviceString)

        }
    }
    if serviceString == ""{
        self.servicesBtnLbl.text = "Tap to select"
    }else{
        self.servicesBtnLbl.text = serviceString

    }

它正在运行中,希望对您有所帮助。

你应该更新你的

tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)方法类似

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath) as! switchTableViewCell
        if cell.accessoryType == .checkmark {
            cell.accessoryType = .none
        } else {
            cell.accessoryType = .checkmark
        }
}

但是,如果您希望单元格即使在重新加载后仍保持其附件类型,则必须创建一个数组来跟踪每个单元格的附件类型,并在didSelectRowAt()然后在UI中更新附件类型时在数组中更新其值。 cellForRowAt()您必须使用此数组来设置每个单元格的annexType。

暂无
暂无

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

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