从数组

时间:2015-07-07 09:21:00

标签: arrays xcode swift uitableview

我有一个日期数组,我从下载的JSON文件中获取。数据将显示在UITableViewController中。此数组还包含表的各个部分。我已经创建了另一个数组,可以在各自的数组中获取这些部分。我现在想把它放在表格中,但我不知道如何用章节来做。我没有任何部分做了同样的事情,所以只需要帮助来实现它。我的完整代码如下:

import UIKit

class CalTableViewController: UITableViewController {


var bytes: NSMutableData = NSMutableData()

var numberElements: Int = Int()

var sectionArray: NSMutableArray = NSMutableArray()

var headlinesLabels:NSMutableArray = NSMutableArray ()

var summaryLabels:NSMutableArray = NSMutableArray ()

var linkLabels:NSMutableArray = NSMutableArray ()

var photoLabels:NSMutableArray = NSMutableArray ()

var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()

var refreshHeaderView: PZPullToRefreshView?

var reloading: Bool = false

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    receiveCalendarDates()
}

func receiveCalendarDates() {
            let request = NSMutableURLRequest(URL: NSURL(string: "https://www.someurl.com")!)
    request.setValue("Bearer some token", forHTTPHeaderField: "Authorization")
    _ = NSURLConnection(request: request, delegate: self, startImmediately: true)
    let barButton: UIBarButtonItem = UIBarButtonItem(customView: activityIndicator)
    self.navigationItem.setRightBarButtonItem(barButton, animated: true)
    activityIndicator.hidesWhenStopped = true
    activityIndicator.startAnimating()
    print("Request sent")
}

func connection(connection: NSURLConnection!, didReceiveData conData: NSData!) {
    self.bytes.appendData(conData)
    print("Loading")

    headlinesLabels = NSMutableArray ()

    summaryLabels = NSMutableArray ()

    linkLabels = NSMutableArray ()

    photoLabels = NSMutableArray ()
}

func connection(didReceiveResponse: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
    self.bytes = NSMutableData()
    print("Loading2")
}

func connection(connection: NSURLConnection, didFailWithError error: NSError) {
    print(error)
    print("Something Went Wrong")
}


func connectionDidFinishLoading(connection: NSURLConnection!) {

    let jsonresult:NSDictionary = try! NSJSONSerialization.JSONObjectWithData(self.bytes, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary

    print(jsonresult)

    let results: NSDictionary = jsonresult["results"] as! NSDictionary
    let collection1: NSMutableArray = results["collection1"] as! NSMutableArray
    let sectionsArray: NSMutableArray = []

    for (index, element) in collection1.enumerate() {
        let dateString = element["date"] as! String
        if dateString == "" {
            collection1.removeObjectAtIndex(index)
        }
        else if dateString != "" {
            let dateStringUpper = dateString.uppercaseString
            if dateString == dateStringUpper || dateString.rangeOfString("Term") != nil {
                sectionsArray.addObject(dateString)
            }
        }
    }
    let collectionNum = collection1.count
    numberElements = collectionNum

    print(collectionNum)
    print(sectionsArray)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return sectionArray.count
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return numberElements
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath)

    // Configure the cell...


    return cell
}

connectionDidFinishLoading之后两个数组的内容:

的示例日期和事件
collection1 =                         
(
{
                date = "Term 3 (9 weeks)";
                event = "";
                index = 1;
            },
{
                date = "Thur 3";
                event = "Year 9 Dinner Dance";
                index = 58;
            },
{
                date = "Thur 3";
                event = "Last Day Term 4";
                index = 59;
            }
        );

sectionsArray的数组:

sectionsArray = ("Term 3 (9 weeks)", "JULY", "AUGUST", "SEPTEMBER", "Term 4 (9 weeks)", "OCTOBER", "NOVEMBER", "DECEMBER")

所以sectionsArray项目仍然在collection1中作为日期,但不是它们成为一个单元格我希望这些项目成为表格中的一个部分。

欢迎所有建议:)

如有必要,请提出问题。

0 个答案:

没有答案