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

时间:2016-02-27 12:18:41

标签: ios swift uitableview

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

public static void main (String[] args) {
  String data = "calls 'create' using 'POST' on some uri";
  String[] dataArray = new String[2];
  Matcher matcher = Pattern.compile("'[a-zA-Z]+'").matcher(data);
  int counter = 0;
  while (matcher.find()) {
    String result = matcher.group(0);
    dataArray[counter++] = result.substring(1, result.length() - 1);
  }
}

任何建议,
提前谢谢

1 个答案:

答案 0 :(得分:2)

这是更新后的代码。感谢@pbasdf Sir支持与指导:)

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()
});
}