I have an array of dates that I get from a JSON file that is downloaded. The data will be displayed in a UITableViewController. This array also contains the sections for the table. I have made another array that gets the sections in their own array. I now want to put this in the table but I don't know how to do it with sections. I have done the same thing without sections so just need help to implement it. My full code below:
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
}
Contents of two arrays after connectionDidFinishLoading:
Example dates and events of
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;
}
);
The array for sectionsArray:
sectionsArray = ("Term 3 (9 weeks)", "JULY", "AUGUST", "SEPTEMBER", "Term 4 (9 weeks)", "OCTOBER", "NOVEMBER", "DECEMBER")
So the sectionsArray items are still in the collection1 as dates as well but instead of them becoming a cell I want those items to be a section in the table.
All suggestions welcomed :)
Please ask questions if necessary.