gpt4 book ai didi

ios - 使用静态和动态数据填充 UITableview 部分

转载 作者:搜寻专家 更新时间:2023-11-01 07:26:41 26 4
gpt4 key购买 nike

我在 UITableview 中有三个部分,即类别、我的帐户和支持是我的帐户,支持部分填充了静态数据,但类别部分将在 Alamofire 和 SwiftyJSON 的帮助下填充 Web api 响应我得到我想要但无法弄清楚如何填充特定部分的结果这是我的代码...

import UIKit
import Alamofire
import SwiftyJSON

class MenuView: UIViewController, KYDrawerControllerDelegate,UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var menuTableview: UITableView!

struct Objects {
var sectionName : String!
var sectionObjects : [String]!
}
var objectsArray = [Objects]()
var categoriesArr = [String]()
override func viewDidLoad() {
super.viewDidLoad()

let bar:UINavigationBar! = self.navigationController?.navigationBar
//self.title = "Home Screen"
bar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
bar.shadowImage = UIImage()
bar.alpha = 0.0

objectsArray = [
Objects(sectionName: "", sectionObjects: ["Home"]),
Objects(sectionName: "Categories", sectionObjects: categoriesArr),
Objects(sectionName: "My Account", sectionObjects: ["My WishList", "My Profile", "My Addresses", "My Order", "Log out"]),
Objects(sectionName: "Support", sectionObjects: ["About Us", "Delivery Information", "Privacy Policy", "Terms & Conditions", "Contact Us", "Return Policy"])]

}

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)

callAPI()
}
//MARK: UITabView DataSources

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell!


cell.textLabel?.text = objectsArray[indexPath.section].sectionObjects[indexPath.row]
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objectsArray[section].sectionObjects.count
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return objectsArray.count
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return objectsArray[section].sectionName
}

func callAPI () {
//SwiftSpinner.show("Sending request..")

Alamofire.request(.POST, "http://www.picknget.com/webservice/index.php/Home/get_all_category")
.responseJSON { response in
if let value = response.result.value {
let json = JSON(value)
if let _statusCode = json["status"].string {
print("the ststus code is ", _statusCode)
if (_statusCode == "1"){
self.parseJSON(json)
}
else {
self.callAlert("Alert", _msg: "Something Went Wrong Kindly Check Your Connection & Try Agian")
}
}
//print ("json result ", json)

}
}.responseString { response in
//print("response ",response.result.value)
}
}


func parseJSON(json: JSON) {

for result in json["category"].arrayValue {
print("The available categories are",result["MainCatName"].stringValue)
self.categoriesArr.append(result["MainCatName"].stringValue)
}

print("@@@@@@@@")
objectsArray[2].sectionObjects = categoriesArr
print(categoriesArr.count)

print(categoriesArr[0],categoriesArr[1])
dispatch_async(dispatch_get_main_queue(),{
self.menuTableview.reloadData()
});
}

任何建议,
提前谢谢你

最佳答案

这是有效的更新代码。感谢@pbasdf 先生的支持和指导:)

import UIKit
import Alamofire
import SwiftyJSON

class MenuView: UIViewController, KYDrawerControllerDelegate,UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var menuTableview: UITableView!

struct Objects {
var sectionName : String!
var sectionObjects : [String]!
}
var objectsArray = [Objects]()
var categoriesArr = [String]()
override func viewDidLoad() {
super.viewDidLoad()

let bar:UINavigationBar! = self.navigationController?.navigationBar
//self.title = "Home Screen"
bar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
bar.shadowImage = UIImage()
bar.alpha = 0.0

objectsArray = [
Objects(sectionName: "", sectionObjects: ["Home"]),
Objects(sectionName: "Categories", sectionObjects: categoriesArr),
Objects(sectionName: "My Account", sectionObjects: ["My WishList", "My Profile", "My Addresses", "My Order", "Log out"]),
Objects(sectionName: "Support", sectionObjects: ["About Us", "Delivery Information", "Privacy Policy", "Terms & Conditions", "Contact Us", "Return Policy"])]

}

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)

callAPI()
}
//MARK: UITabView DataSources

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell!


cell.textLabel?.text = objectsArray[indexPath.section].sectionObjects[indexPath.row]
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objectsArray[section].sectionObjects.count
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return objectsArray.count
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return objectsArray[section].sectionName
}

func callAPI () {
Alamofire.request(.POST, "http://www.picknget.com/webservice/index.php/Home/get_all_category")
.responseJSON { response in
if let value = response.result.value {
let json = JSON(value)
if let _statusCode = json["status"].string {
print("the ststus code is ", _statusCode)
if (_statusCode == "1"){
self.parseJSON(json)
}
else {
self.callAlert("Alert", _msg: "Something Went Wrong Kindly Check Your Connection & Try Agian")
}
}
//print ("json result ", json)

}
}.responseString { response in
//print("response ",response.result.value)
}
}


func parseJSON(json: JSON) {

for result in json["category"].arrayValue {
print("The available categories are",result["MainCatName"].stringValue)
self.categoriesArr.append(result["MainCatName"].stringValue)
}

print("@@@@@@@@")
objectsArray[2].sectionObjects = categoriesArr
print(categoriesArr.count)

print(categoriesArr[0],categoriesArr[1])
dispatch_async(dispatch_get_main_queue(),{
self.menuTableview.reloadData()
});
}

关于ios - 使用静态和动态数据填充 UITableview 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35669684/

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