我正在尋找使用自定義對象填充UITableView,而不必使用多維數組來處理這些部分。我想我所缺少的是一種將對象進行排序的邏輯。該對象基本上是一個字典,我有var section: Int
或者設置爲0
或1
。這些對象當前存儲在數組中並加載到表視圖中。其他變量是各種各樣的信息。UITableView與部分和Swift中的自定義對象
我有一個對象,看起來像這樣:
class MenuModel {
var displayText: String
var displayImage: UIImage
var location: String
var otherData: String
var section: Int
}
調用是這樣的:
let bc = MenuModel(displayText: "Basic Commands", displayImage: UIImage(named: "[email protected]")!, location: "commands", otherData: "", section: 0)
self.MenuList.append(bc) //Right now I'm appending it to an array called MenuList
我如何可以訪問MenuModel.section並得到一個結果的對象爲那些部分進行排序0
或1
?這是否需要在tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
這部分是更具體到我目前的實施。這並不完美(現在甚至可以正常工作)界面已更新並且看起來很開心,但是,即使完成處理程序只運行一次,MenuList
陣列也會呈現四次。
這是我正在運行的代碼。基本上有兩個網絡呼叫,一個呼叫到Firebase,一個呼叫通過Alamofire。
public func ConstructMenu(completion: (Bool) ->()) {
if let user = FIRAuth.auth()?.currentUser {
// User is signed in.
GlobalFirebase.UserRef.child(user.uid).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
let serverAdmin = snapshot.value!["serverAdmin"] as! String
if serverAdmin != "none" {
self.log.info("User as a Server Admin signed in")
let bc = MenuModel(displayText: "Basic Commands", displayImage: UIImage(named: "[email protected]")!, location: "commands", otherData: "", section: 0)
let ca = MenuModel(displayText: "Console Access", displayImage: UIImage(named: "[email protected]")!, location: "console", otherData: "", section: 0)
let sm = MenuModel(displayText: "Server Management", displayImage: UIImage(named: "[email protected]")!, location: "management", otherData: "", section: 0)
let cs = MenuModel(displayText: "Connect and Share", displayImage: UIImage(named: "[email protected]")!, location: "connect", otherData: "", section: 0)
self.MenuList.append(bc, ca, sm, cs)
completion(true)
} else {
self.log.info("User not a Server Admin signed in")
let ns = MenuModel(displayText: "New Server", displayImage: UIImage(named: "[email protected]")!, location: "makeServer", otherData: "", section: 0)
self.MenuList.append(ns)
completion(true)
}
})
} else {
self.log.info("No user is signed in.")
let ns = MenuModel(displayText: "New Server", displayImage: UIImage(named: "[email protected]")!, location: "makeServer", otherData: "", section: 0)
self.MenuList.append(ns)
completion(true)
}
//Check for Other Archetype Servers
//Query Multicraft
let commandDictionary = [ "APIuser" : GlobalConstants.MulticraftAPIUser,
"APIkey" : "\(GlobalConstants.MulticraftAPIKey)",
"APIreq" : "listServers"]
Alamofire.request(.POST, "\(GlobalConstants.MulticraftAPIURL)tworeq.php", parameters: commandDictionary)
.responseJSON(){ response in
self.log.info("Request: \(response.result.value)")
if let _ = response.result.value {
if (JSON(response.result.value!)["success"] == true) {
let json = JSON(response.result.value!)
// self.log.verbose(json["data"]["Servers"].dictionaryValue)
for (key, value) in json["data"]["Servers"].dictionaryValue {
self.log.verbose("\"\(key) : \(value.string!)\"")
var name = "\(value.string!)"
var serverID = "\(key)"
var image = UIImage(named: "server_\(Int(arc4random_uniform(4) + 1))")!
var item = MenuModel(displayText: name, displayImage: image, location: "serverInfo", otherData: serverID, section: 1)
self.MenuList.append(item)
}
completion(true)
} //If result succeeds
}
} // Alamofire Request
}
,我調用該代碼:
override public func viewWillAppear(animated: Bool) {
MenuList.removeAll()
self.ConstructMenu() {completion in completion
if completion == true {
self.tableView.reloadData()
print("Menu Setup Completed")
} else {
self.tableView.reloadData()
print("Menu Setup Failed")
}
}
謝謝!這個方法取得了訣竅。我不得不將它添加到幾個不同的地方,以使UI匹配,但我期待着。 :] – ErrorCode3000