gpt4 book ai didi

ios - 如何在 UITableView 中将注释附加到不同的 UICells

转载 作者:搜寻专家 更新时间:2023-11-01 06:52:44 25 4
gpt4 key购买 nike

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

我将在这里附上一个关于我的问题的小项目:AppendCommentsToCells

这是我的模型的代码:

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

这是我的 View Controller :

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: "📝 Add Comment", style: .default) { action in
self.performSegue(withIdentifier: "goChecklistAddComment", sender: nil)
}

let actionSheet = configureActionSheet()
actionSheet.addAction(addComment)

self.present(actionSheet, animated: true, completion: nil)
}

func configureActionSheet() -> UIAlertController {
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
actionSheet.addAction(cancel)

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad ){
actionSheet.popoverPresentationController?.sourceView = self.view
actionSheet.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
actionSheet.popoverPresentationController?.permittedArrowDirections = []
}

return actionSheet
}
}

// Receive Comments using the Delegate
extension ChecklistVC: ChecklistAddCommentDelegate {

func receiveVehicleComment(vehicleComment: String?, trailerComment: String?) {
vehicleCommentReceived = vehicleComment ?? String()
trailerCommentReceived = trailerComment ?? String()
questionsTableView.reloadData()
}

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

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

这里我附上了我的问题的截图:

enter image description here

感谢您抽空阅读本文。

最佳答案

2 个变量 vehicleCommentReceived ,trailerCommentReceived 应该是每个模型对象中的属性

class ChecklistItem {

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

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

}

当您编辑单元格时,请确保存储它的 indexPath,然后在保存时更改特定 indexPath 部分/行的模型数组


细胞内部

// Detect when the user press Long Tap on any cell
@objc func tapEdit(sender: UITapGestureRecognizer) {
delegate?.tapGestureOnCell(self)
}

在vc里面声明

 var lastIndexPath:IndexPath!

然后这样实现

func tapGestureOnCell(_ cell:ChecklistCell) {

showOptionsOnCellTapped(questionsTableView.indexPath(for: cell)!)
}

func showOptionsOnCellTapped(_ indexPath:IndexPath){

let addComment = UIAlertAction(title: "📝 Add Comment", style: .default) { action in
self.lastIndexPath = indexPath
self.performSegue(withIdentifier: "goChecklistAddComment", sender: nil)
}

let actionSheet = configureActionSheet()
actionSheet.addAction(addComment)
self.present(actionSheet, animated: true, completion: nil)
}

func receiveVehicleComment(vehicleComment: String?, trailerComment: String?) {
let item = itemSections[lastIndexPath.section].checklistItems[lastIndexPath.row]
item.vehicleComment = vehicleComment ?? ""
item.trailerComment = trailerComment ?? ""
}

然后在 cellForRowAt

里面
cell.vehicleCommentLabel.text = "Vehicle comment: \(item.vehicleComment)" 
cell.trailerCommentLabel.text = "Trailer comment: \(item.trailerComment)"

关于ios - 如何在 UITableView 中将注释附加到不同的 UICells,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56021465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com