// 我有一個 Notes 的結構模型
struct NotesModel{
var groupName: String // sections in tableView
var info: [NotesInfo] // rows in respective sections in tableView
}
struct NotesInfo{
var groupName: String
var image: String
var notesTitle: String
var notesDescription: String
var notesDate: String
}
// 1) this is my ViewController(FirstVC)
var arrayNotes = [NotesModel]() // array that will be used to populate tableView
var info1 = [NotesInfo]()
var info2 = [NotesInfo]()
var info3 = [NotesInfo]()
// I have appended data in arrayNotes
info1.append(NotesInfo(groupName: "ABC", image: "img1", notesTitle: "Public Notes", notesDescription: "Public Notes are for xyz use...", notesDate: "17/08/2020"))
info1.append(NotesInfo(groupName: "ABC", image: "img1", notesTitle: "Public Notes(A)", notesDescription: "Public Notes are for xyz use...", notesDate: "19/08/2020"))
arrayNotes.append(NotesModel(groupName: "ABC", info: info1))
info2.append(NotesInfo(groupName: "XYZ", image: "img2", notesTitle: "My Notes", notesDescription: "My Notes include...", notesDate: "25/08/2020"))
arrayNotes.append(NotesModel(groupName: "XYZ", info: info2))
info3.append(NotesInfo(groupName: "PQR", image: "img3", notesTitle: "Notes Example", notesDescription: "Notes Example here..", notesDate: "25/08/2020"))
arrayNotes.append(NotesModel(groupName: "PQR", info: info2))
// I have a TableView on ViewController to present NotesModel data
// MARK: - Number of Sections in TableView
func numberOfSections(in tableView: UITableView) -> Int {
return arrayNotes.count // returns 3
}
// MARK: - HeaderView in Section
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = Bundle.main.loadNibNamed("HeaderCell", owner: self, options: nil)?.first as! HeaderCell
let dict = arrayNotes[section]
headerCell.lblGroupName.text = dict.groupName // "ABC", "XYZ", "PQR"
let info = dict.info
for values in info{
headerCell.imgGroup.image = UIImage(named: values.image) // "img1", "img2", "img3"
}
return headerCell
}
// MARK: - Number of Rows in TableView
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayNotes[section].info.count // "ABC" -> 2 rows, "XYZ" -> 1 row, "PQR" -> 1 row
}
// MARK: - Cell For Row At in TableView
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "NotesCell", for: indexPath) as! NotesCell
cell.selectedBackgroundView = UIView()
cell.selectedBackgroundView?.backgroundColor = .systemBackground
let dict = arrayNotes[indexPath.section]
let info = dict.info[indexPath.row]
cell.lblNotesTitle.text = info.notesTitle
cell.lblNotesDescription.text = info.notesDescription
cell.lblNotesDate.text = info.notesDate
return cell
}
// MARK: - Button Create New Note Event
func createNewNotesButton(_ sender: UIButton){
let storyBoard = UIStoryboard(name: "NotesStoryBoard", bundle: nil)
let createNewNotesVC = storyBoard.instantiateViewController(withIdentifier: "CreateNewNotesVC") as! CreateNewNotesVC
createNewNotesVC.delegate = self
self.navigationController?.pushViewController(createNewNotesVC, animated: true)
}
// MARK: - Create New Notes Protocol Conformance
func passNewNotesModel(object: NotesModel) {
for item in arrayNotes{
if item.groupName == object.groupName{
print("same section")
var ogInfo = item.info
let addedInfo = object.info
ogInfo.append(contentsOf: addedInfo)
print("ogInfo after appending --> \(ogInfo)")
arrayNotes.append(NotesModel(groupName: item.groupName, info: ogInfo))
// I want to append rows in section that already exist on ViewController, there is already 3 sections with different number of rows but this doesn't append in respective section but creates new section every time
}else{
print("different section")
arrayNotes.append(object)
// and if section doesn't exist here on ViewController then create new section and add rows in it. but this adds multiple sections
}
}
self.tableViewNotes.reloadData()
}
這是我的 CreateNewNotesVC(SecondVC)
// MARK: - 協議為 NotesModel 創建新注釋
協議 CreateNewNotesModel { func passNewNotesModel(object: NotesModel) }
var newNotesModel:NotesModel!
var 委托:CreateNewNotesModel?
這里我有 textFields 和“SUBMIT”按鈕,單擊“SUBMIT”按鈕,將 NotesModel 物件和 popView 傳遞給 firstVC,firstVC 上的 tableView 應該使用新創建的模型物件進行更新。
這里還有一個下拉選單,我必須從中選擇筆記的組名說有6個不同的組名,代表tableView中的部分,如果我從下拉選單中選擇“ABC”組名,然后在ViewController(firstVC)資料上應該附加在tableView的“ABC”部分中,如果我從下拉串列中選擇“OMG”組名,而ViewController(firstVC)上不存在,那么應該創建新部分。
// MARK: - Button Submit Event
@IBAction func btnSubmit_Event(\_ sender: Any) {
var info = [NotesInfo]()
info.append(NotesInfo(groupName: txtSelectGroup.text ?? "", image: ImageString, notesTitle: txtNotesTitle.text ?? "", notesDescription: txtNotesDescription.text ?? "", notesDate: txtNotesDate.text ?? ""))
newNotesModel = (NotesModel(groupName: txtSelectGroup.text ?? "", info: info))
delegate?.passNewNotesModel(object: newNotesModel)
self.navigationController?.popViewController(animated: true)
}
我想在各個部分中附加資料,由于for回圈,我的代碼每次都會生成部分,我不知道如何在各個部分中附加資料,任何幫助將不勝感激!謝謝你。
// UPDATE: as per WeyHan Ng suggestion:
for item in arrayNotes{
var ogInfo = item.info
let addedInfo = object.info
ogInfo.append(contentsOf: addedInfo)
print("ogInfo after appending --> \(ogInfo)")
if item.groupName == object.groupName{
print("same section")
let index = arrayNotes.firstIndex(where: { $0.groupName == object.groupName})!
print(index)
arrayNotes[index].info = ogInfo // this is solved
}else{
print("different section")
arrayNotes.append(object) // ISSUE: appending more than one section every time
}
}
self.tableViewConsultantMilestones.reloadData()
uj5u.com熱心網友回復:
根據您passNewNotesModel(object: newNotesModel)方法中的代碼,您正在執行以下操作:
Iterate through every element in 'arrayNotes' as 'item'.
If the element 'groupName' of 'item' matches the 'groupName' of 'object'
Make a copy of 'item.info' as 'ogInfo'
Make a copy of 'object.info' as 'addedInfo'
Append 'addedInfo' to 'ogInfo'
Append 'ogInfo' to 'arrayNotes'
Else
Append 'object' to 'arrayNotes'
End if
End loop
因此,對于arrayNotesN 個元素,您的代碼追加objectN-1 次并追加 1 次ogInfo addedInfo
你需要做的是:
Find index of the element with 'groupName' equal to 'object.groupName'
If found
Replace 'arrayNotes[foundIndex]' with 'arrayNotes[foundIndex] object.info'
Else
Append 'object' to 'arrayNotes'
End if
提示:您可以使用first(where: (String) throws -> Bool)方法查找元素arrayNotes并使用它的回傳值替換元素。
uj5u.com熱心網友回復:
// 更新
// 下面的代碼對我有用
// MARK: - Create New Notes Protocol Conformance
func passNewNotesModel(object: NotesModel) {
let filteredDict = self.arrayNotes.filter({$0.groupName == object.groupName})
if filteredDict.count > 0 {
// add rows in that particular section
for indexValue in arrayNotes.enumerated() {
if indexValue.element.groupName == filteredDict.first?.groupName {
if let obj = filteredDict.first {
var ogInfo = obj.info
let addedInfo = object.info
ogInfo.append(contentsOf: addedInfo)
arrayNotes[indexValue.offset].info = ogInfo
}
}
}
} else {
// add new section
arrayNotes.append(object)
}
}
uj5u.com熱心網友回復:
// 更新
// WeyHan Ng 的建議對我有用:
// MARK: - Create New Notes Protocol Conformance
func passNewNotesModel(object: NotesModel) {
let index = arrayNotes.firstIndex(where: { $0.groupName == object.groupName})
print(index ?? 0)
if index != nil{
var ogInfo = arrayNotes[index ?? 0].info
let addedInfo = object.info
ogInfo.append(contentsOf: addedInfo)
arrayNotes[index ?? 0].info = ogInfo
}else{
arrayNotes.append(object)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/533082.html
