我正在使用包含变量的struct 'xyz'
。这些变量是array
'arr_InStruct
'的一部分。 “ arr_InStruct
”的副本在TVC中用作dataSource
。
这是一个有效的程序,但我对解决方案不满意。
缺点1:我需要知道变量'indexPath
'中的确切位置(arr_InStruct
)
缺点2:变得非常拥挤,难以理解
关于如何以更智能的方式更新array
的任何建议?
数据文件
import Foundation
struct xyz {
//-> Variablen für Array
//didSet updates the array in struct after value of variable was changed
static var text0:String = "Entry 0" {didSet{arr_InStruct[0][0] = text0}}//push update 'arr_InStruct' bei Änderung
static var text1:String = "Entry 1" {didSet{arr_InStruct[0][1] = text1}}//push update 'arr_InStruct' bei Änderung
static var text2:String = "Entry 2" {didSet{arr_InStruct[0][2] = text2}}//push update 'arr_InStruct' bei Änderung
static var text3:String = "Entry 3" {didSet{arr_InStruct[0][3] = text3}}//push update 'arr_InStruct' bei Änderung
//<- Variablen für Array
//disadvantage 1: I need to know the exact position (indexPath) of the variable within 'arr_InStruct'
//disadvantage 2: becomes very crowded und unreadable
//is there a smarter way to archive an array update?
//2D Array
//es wird gefüllt mit dem Inhalt der obigen Variablen
static var arr_InStruct:[[String]] =
[
[
text0,text1,text2,text3,]
]//end arr_InStruct
}//end struct xyz
TVC
import UIKit
class reloadTVTest: UITableViewController {
//MARK: - >>> Arrays w data for TV
let arr_Header = ["Section 0"] //1D Array
var arr_Data = xyz.arr_InStruct { //2D Array in 'ArrayFile.swift' -> struct 'xyz'
didSet{ //didSet will be called every time you change something in your array
DispatchQueue.main.async {self.tableView.reloadData()}
}//end didSet
}//end var
//MARK: <<< Arrays w data for TV
//MARK: - >>> actions
@IBaction func changepressed(_ sender: UIBarButtonItem) {
print("""
changepressed
arr_Data in TVC: \(arr_Data) <- Array in TVC before change
xyz.arr_InStruct: \(xyz.arr_InStruct) <- Array in struct before change
""")
xyz.text1 = "Entry changed "+randomString(length: 3) //change Variable in 'xyz'
print("""
xyz.text1: '\(xyz.text1)' <- Variable changed in struct,didset called
xyz.arr_InStruct: \(xyz.arr_InStruct) <- Array in struct changed by didSet
arr_Data in TVC: \(arr_Data) <- but Array in TVC not changed
""")
arr_Data = xyz.arr_InStruct //Array changed,didset called -> reloadData()
print("""
arr_Data in TVC was updated
arr_Data in TVC: \(arr_Data) <- now Array in TVC changed,didSet updated TV
""")
}
//MARK: <<< actions
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return arr_Header.count
}
override func tableView(_ tableView: UITableView,viewForHeaderInSection section: Int) -> UIView? {
let header = UILabel()
header.text = arr_Header[section]
return header
}
override func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return arr_Data[section].count
}
override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellreloadTVTest",for: indexPath)
let name = arr_Data[indexPath.section][indexPath.row]
// Configure the cell...
cell.textLabel?.text = name
return cell
}
}//end class reloadTVTest
extension reloadTVTest { //Swift 4.2,creating random String
func randomString(length: Int) -> String {
let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
return String((0..<length).map{ _ in letters.randomElement()! })
}
}//end extension reloadTVTest