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

标签 ios swift uitableview

我在 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/

相关文章:

swift - 自定义 tableViewCell 高度

ios - 如何进一步缩短三元运算符

ios - 结合-OpenGL-ES-Cocos2d 功能-和-OpenGL-ES-(Unity 3d) 导航--在-iOS-app 内

ios - 快速单击按钮时出错

ios - 如何删除或查看 TableView 行

iphone - 设置分组 UITableViewCell 的背景颜色

ios - uicollectionview 在重新加载数据后立即选择一个项目?

ios - Itunes 连接 - 商标名称?

arrays - 创建一个用于在 Swift 中打印出玩家姓名和分数的 for-in 循环

ios - 以编程方式将 UITableViewCell 添加到 UITableView