Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何将标记从一个UITableView添加到另一个UITableView_Ios_Swift_Uitableview - Fatal编程技术网

Ios 如何将标记从一个UITableView添加到另一个UITableView

Ios 如何将标记从一个UITableView添加到另一个UITableView,ios,swift,uitableview,Ios,Swift,Uitableview,我有2个ViewController,每个ViewController都有一个UITableView。 在MainViewController中,我有几行,我想为每行添加第二个ViewController中的不同标记。 我的标记保存在字典中(我不知道这是否是最好的方法,但我在想,也许我可以避免使用Dict而不是数组两次附加标记)。 问题是我没有正确地附加所选的标记,我不知道该怎么做。 在这里,我创建了一个反映我的问题的小项目: 以下是主VC的代码: class ChecklistVC: UIVi

我有2个
ViewController
,每个ViewController都有一个
UITableView
。 在MainViewController中,我有几行,我想为每行添加第二个ViewController中的不同标记。 我的标记保存在字典中(我不知道这是否是最好的方法,但我在想,也许我可以避免使用Dict而不是数组两次附加标记)。 问题是我没有正确地附加所选的标记,我不知道该怎么做。 在这里,我创建了一个反映我的问题的小项目:

以下是主VC的代码:

class ChecklistVC: UIViewController {

    @IBOutlet weak var questionsTableView: UITableView!

    //Properties
    lazy var itemSections: [ChecklistItemSection] = {
        return ChecklistItemSection.checklistItemSections()
    }()
    var lastIndexPath: IndexPath!
    var selectedIndexPath: IndexPath!

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)

        questionsTableView.reloadData()
    }
}

extension ChecklistVC: UITableViewDelegate, UITableViewDataSource {

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

        let itemCategory = itemSections[section]
        return itemCategory.checklistItems.count
    }

    func numberOfSections(in tableView: UITableView) -> Int {

        return itemSections.count
    }

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

        let cell = tableView.dequeueReusableCell(withIdentifier: "checklistCell", for: indexPath) as! ChecklistCell

        let itemCategory = itemSections[indexPath.section]
        let item = itemCategory.checklistItems[indexPath.row]
        cell.delegate = self
        cell.configCell(item)

        cell.vehicleCommentLabel.text = item.vehicleComment
        cell.trailerCommentLabel.text = item.trailerComment

        cell.tagNameLabel.text = item.vehicleTags[indexPath.row]?.name

        return cell
    }


    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if segue.identifier == "goChecklistAddComment" {
            let addCommentVC = segue.destination as! ChecklistAddCommentVC
            addCommentVC.delegate = self
        }

        if segue.identifier == "goChecklistAddTag" {
            let checklistAddTag = segue.destination as! ChecklistAddTagVC

            checklistAddTag.indexForSelectedRow = self.selectedIndexPath

            checklistAddTag.tagsCallback = { result in
                print("result: \(result)")
                let item = self.itemSections[self.lastIndexPath.section].checklistItems[self.lastIndexPath.row]
                item.vehicleTags = result
            }
        }
    }
}

以下是标签ViewController的代码:

class ChecklistAddTagVC: UIViewController {

    // Interface Links
    @IBOutlet weak var tagsTitleLabel: UILabel!
    @IBOutlet weak var tagsTableView: UITableView!

    // Properties
    var tagsDictionary: [Int: Tag] = [:]
    var tagsAdded: [Int:Tag] = [:]
    var tagsCallback: (([Int:Tag]) -> ())?
    var indexForSelectedRow: IndexPath!

    override func viewDidLoad() {
        super.viewDidLoad()
        tagsTableView.tableFooterView = UIView()

        tagsDictionary = [
            1: Tag(remoteID: 1, categoryID: 1, name: "Tag1", colour: "red"),
            2: Tag(remoteID: 2, categoryID: 1, name: "Tag2", colour: "blue"),
            3: Tag(remoteID: 3, categoryID: 1, name: "Tag3", colour: "orange"),
            4: Tag(remoteID: 4, categoryID: 1, name: "Tag4", colour: "black")
        ]

        print("Received index for SelectedRow: \(indexForSelectedRow ?? IndexPath())")
    }
}

extension ChecklistAddTagVC: UITableViewDelegate, UITableViewDataSource {

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "defectAndDamageTagCell", for: indexPath) as! ChecklistAddTagCell
        cell.configCell()
        cell.delegate = self
        cell.tagNameLabel.text = tagsDictionary[indexPath.row + 1]?.name.capitalized
        return cell
    }

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

        return 60
    }
}

extension ChecklistAddTagVC: ChecklistAddTagCellDelegate{

    // When the user press Add Tag then will be added in a dictionary and sent to ChecklistVC using a callback closure.
    func addTagBtnPressed(button: UIButton, tagLabel: UILabel) {

        if button.currentTitle == "+"{
            button.setTitle("-", for: UIControl.State.normal)
            tagLabel.textColor = UIColor.orange
            tagsAdded = [0: Tag(remoteID: 1, categoryID: 1, name: tagLabel.text ?? String(), colour: "red")]
            print(tagsAdded[0]?.name ?? String())
            tagsCallback?(tagsAdded)
        }
        else{
            button.setTitle("+", for: UIControl.State.normal)
            tagLabel.textColor = UIColor.black
            tagsAdded.removeValue(forKey: 0)
            print(tagsAdded)
            tagsCallback?(tagsAdded)
        }
    }
}

以下是我的问题的捕获:


谢谢你阅读这篇文章

能否尝试处理tableview:didSelectRowatineXpath而不是使用segue,并在didSelectRowatineXpath上显示添加标记vc。

我修复了它

解决方案如下。您还可以通过以下链接找到已完成的项目:

MainVC:

类ChecklistVC:UIViewController{
@IBMOutlet弱var问题稳定视图:UITableView!
//性质
lazy var ItemSection:[ChecklistItemSection]={
返回ChecklistItemSection.checklistItemSections()
}()
var lastIndexPath:indepath!
重写func viewDidLoad(){
super.viewDidLoad()
}
覆盖函数视图将出现(uo动画:Bool){
super.viewwillbeen(true)
questionsTableView.reloadData()
}
}
扩展ChecklistVC:UITableViewDelegate,UITableViewDataSource{
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
设itemCategory=itemSections[节]
return itemCategory.checklistItems.count
}
func numberOfSections(在tableView:UITableView中)->Int{
返回itemSections.count
}
//设置每个节的标题
func tableView(tableView:UITableView,titleForHeaderInSection:Int)->String{
让checklistItemCategory=itemSections[节]
返回checklistItemCategory.name.uppercased()
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
让cell=tableView.dequeueReusableCell(标识符为:“checklistCell”,for:indexath)作为!checklistCell
让itemCategory=itemSections[indexPath.section]
let item=itemCategory.checklistItems[indexPath.row]
cell.delegate=self
cell.configCell(项目)
cell.vehicleCommentLabel.text=item.vehicleComment
cell.trailerCommentLabel.text=item.trailerComment
让sortedTagNames=item.vehicleTags.keys.sorted(按:{$0<$1}).compactMap({item.vehicleTags[$0]})
打印(“排序标记名:\(sortedTagNames.map{$0.name})”)
让joinedTagNames=sortedTagNames.map{$0.name}.joined(分隔符:“,”)
cell.tagnamelab.text=joinedTagNames
返回单元
}
func tableView(tableView:UITableView,heightForRowAt indexath:indexPath)->CGFloat{
返回150
}
覆盖功能准备(对于segue:UIStoryboardSegue,发送方:有吗?){
如果segue.identifier==“goChecklistAddComment”{
让addCommentVC=segue.destination为!ChecklistAddCommentVC
addCommentVC.delegate=self
}
如果segue.identifier==“goChecklistAddTag”{
让addTagVC=segue.destination为!ChecklistAddTagVC
addTagVC.delegate=self
addTagVC.addedTags=itemSections[lastIndexPath.section]。checklistItems[lastIndexPath.row]。车辆标签
}
}
}
扩展ChecklistVC:ChecklistCellDelegate{
func tapGestureOnCell(cell:ChecklistCell){
ShowOptionOnCellTapped(questionsTableView.indexPath(for:cell)!)
}
func showOptionsOnCellTapped(uIndeXPath:indexPath){

让addComment=UIAlertAction(标题:“不幸的是,流的要求是这样的。用户点击一个单元格和一个菜单弹出窗口,从那里用户将点击添加标签。