Arrays 从数组中形成表中的节
我有一个从下载的JSON文件中获取的日期数组。数据将显示在UITableViewController中。此数组还包含表的节。我制作了另一个数组,该数组在它们自己的数组中获取节。我现在想把这个放在表中,但我不知道如何处理分区。我已经做了同样的事情,没有部分,所以只需要帮助来实现它。我的完整代码如下:Arrays 从数组中形成表中的节,arrays,xcode,swift,uitableview,Arrays,Xcode,Swift,Uitableview,我有一个从下载的JSON文件中获取的日期数组。数据将显示在UITableViewController中。此数组还包含表的节。我制作了另一个数组,该数组在它们自己的数组中获取节。我现在想把这个放在表中,但我不知道如何处理分区。我已经做了同样的事情,没有部分,所以只需要帮助来实现它。我的完整代码如下: import UIKit class CalTableViewController: UITableViewController { var bytes: NSMutableData = NSM
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
}
ConnectiondFinishLoading后两个数组的内容:
日期和事件示例
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中,但我希望这些项目成为表中的一个部分,而不是成为一个单元格
欢迎所有建议:)
如有必要,请提问。这可能会对您有所帮助:有点帮助,但我无法掌握这些概念并将其实现到我的代码中:(