Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.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中将注释附加到不同的UICell_Ios_Swift_Uitableview - Fatal编程技术网

Ios 如何在UITableView中将注释附加到不同的UICell

Ios 如何在UITableView中将注释附加到不同的UICell,ios,swift,uitableview,Ios,Swift,Uitableview,我有一个UITableView,只有几行(每行代表一个问题),当我按住一个单元格时,我想为每个单元格添加不同的注释。 我有一个模型,其中我硬编码了几个问题。 问题是,当我添加注释时,该注释会添加到我的所有单元格中。我想将注释添加到相应的单元格中,而不是所有单元格。 我该怎么做 我将在此附上一个小项目和我的问题: 以下是我的模型的代码: import Foundation class ChecklistItemSection { var name: String // name of

我有一个
UITableView
,只有几行(每行代表一个问题),当我按住一个单元格时,我想为每个单元格添加不同的注释。 我有一个模型,其中我硬编码了几个问题。 问题是,当我添加注释时,该注释会添加到我的所有单元格中。我想将注释添加到相应的单元格中,而不是所有单元格。 我该怎么做

我将在此附上一个小项目和我的问题:

以下是我的模型的代码:

import Foundation

class ChecklistItemSection {

    var name: String // name of the section
    var checklistItems: [ChecklistItem] // all items from Checklist

    init(named: String, includeChecklistItems: [ChecklistItem]) {

        name = named
        checklistItems = includeChecklistItems
    }

    class func checklistItemSections() -> [ChecklistItemSection] {

        return [vehicleCheck(), viewingScreen(), batteryUnitAndFridge()]
    }

    // Private methods
    private class func vehicleCheck() -> ChecklistItemSection {

        var checklistItems = [ChecklistItem]()

        checklistItems.append(ChecklistItem(templateID: 1, lineID: 1, descript: "Question 1")!)
        checklistItems.append(ChecklistItem(templateID: 2, lineID: 2, descript: "Question 2")!)
        checklistItems.append(ChecklistItem(templateID: 3, lineID: 3, descript: "Question 3")!)

        return ChecklistItemSection(named: "Section 1", includeChecklistItems: checklistItems)
    }

    private class func viewingScreen() -> ChecklistItemSection {

        var checklistItems = [ChecklistItem]()

        checklistItems.append(ChecklistItem(templateID: 4, lineID: 4, descript: "Question 4")!)
        checklistItems.append(ChecklistItem(templateID: 5, lineID: 5, descript: "Question 5")!)
        return ChecklistItemSection(named: "Section 2", includeChecklistItems: checklistItems)
    }

    private class func batteryUnitAndFridge() -> ChecklistItemSection {

        var checklistItems = [ChecklistItem]()

        checklistItems.append(ChecklistItem(templateID: 6, lineID: 6, descript: "Question 6")!)
        checklistItems.append(ChecklistItem(templateID: 7, lineID: 7, descript: "Question 7")!)
        checklistItems.append(ChecklistItem(templateID: 8, lineID: 8, descript: "Question 8")!)
        checklistItems.append(ChecklistItem(templateID: 9, lineID: 9, descript: "Question 9")!)
        return ChecklistItemSection(named: "Section 3", includeChecklistItems: checklistItems)
    }
}


class ChecklistItem {

    var template_id: Int
    var line_id: Int
    var descript: String

    var vehicleComment: String = String()
    var trailerComment: String = String()

    init?(templateID: Int,
          lineID: Int,
          descript: String // Question name
        ) {

        self.template_id = templateID
        self.line_id = lineID
        self.descript = descript
    }
}
这是我的ViewController:

import UIKit

class ChecklistVC: UIViewController {

    @IBOutlet weak var questionsTableView: UITableView!

    //Properties
    var vehicleCommentReceived = String()
    var trailerCommentReceived = String()
    lazy var itemSections: [ChecklistItemSection] = {
        return ChecklistItemSection.checklistItemSections()
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

    }
}

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 = "Vehicle comment: \(vehicleCommentReceived)"

        cell.trailerCommentLabel.text = "Trailer comment: \(trailerCommentReceived)"

        return cell
    }

    // Set the header of each section
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

        let checklistItemCategory = itemSections[section]
        return checklistItemCategory.name.uppercased()
    }

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

        return 110
    }
}

extension ChecklistVC: ChecklistCellDelegate {

    func tapGestureOnCell() {

        showOptionsOnCellTapped()
    }

    func showOptionsOnCellTapped(){

        let addComment = UIAlertAction(title: "The 2 vars  
vehicleCommentReceived ,trailerCommentReceived
should be properties in each model object

class ChecklistItem {

   var template_id: Int
   var line_id: Int
   var descript: String

   var vehicleComment = "" //<<<<< here
   var trailerComment = ""  // <<<<< here

}
导入UIKit
类ChecklistVC:UIViewController{
@IBMOutlet弱var问题稳定视图:UITableView!
//性质
var vehicleComcentreceived=字符串()
var trailerCommentReceived=String()
lazy var ItemSection:[ChecklistItemSection]={
返回ChecklistItemSection.checklistItemSections()
}()
重写func viewDidLoad(){
super.viewDidLoad()
}
}
扩展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,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=“车辆注释:\(vehicleCommentReceived)”
cell.trailerCommentLabel.text=“拖车注释:\(trailerCommentReceived)”
返回单元
}
//设置每个节的标题
func tableView(tableView:UITableView,titleForHeaderInSection:Int)->String{
让checklistItemCategory=itemSections[节]
返回checklistItemCategory.name.uppercased()
}
func tableView(tableView:UITableView,heightForRowAt indexath:indexPath)->CGFloat{
返回110
}
}
扩展ChecklistVC:ChecklistCellDelegate{
func tapGestureOnCell(){
showOptionsOnCellTapped()
}
func showOptionsOnCellTapped(){

let addComment=ui警报操作(标题:“接收到的2 vars
vehiclecommented,trailrCommentReceived
应为每个模型对象中的属性

// Detect when the user press Long Tap on any cell
@objc func tapEdit(sender: UITapGestureRecognizer) {
    delegate?.tapGestureOnCell(self)
}
在vc内部声明这一点

然后像这样实施

func-tappestureoncell(\ucell:ChecklistCell){
ShowOptionOnCellTapped(questionsTableView.indexPath(for:cell)!)
}
func showOptionsOnCellTapped(uIndeXPath:indexPath){

让addComment=UIAlertAction(标题:“您为VehicleComcentreceived创建了全局变量,trailerCommentReceived并将其显示在cellForRowAtIndexPath中。
您需要做的是更新您的数据模型,即特定对象(类类型为ChecklistItemSection)在itemSections数组中。

因此,您的委托方法“receiveVehicleComment”不包含任何有关正在更新的单元格的indexPath的信息。如果包含,您可以在“cellForRow:at”方法中使用该信息来仅更新特定单元格。换句话说,您需要单独跟踪每个车辆的注释,最好是在数组中。因此,
var-vehicleComcentreceived=String()
,应该是
var-vehicleComcentreceived=[String]()
。函数tapGestureOnCell未传播有关被点击单元格的任何信息。您可以通过传递额外的参数单元格类型ChecklistCell来实现此目的,然后在实现委派方法的位置使用indexpath查找单元格的indexpath(for:cell)然后使用此索引仅更新所选的sell。使用indexpath更新了带有注释的数据模型。我的模型中有这两个属性。如何存储相应单元格的indexpath?以及我的
cell.vehicleCommentLabel.text=
应该是什么样子?
 var lastIndexPath:IndexPath!