I have a table view within a UIViewController to display calendar information in table format. It displays the information with one row dedicated to a date then all events on that day undeeath it and so forth. I added a mapper array of ints so i know at what row index each date is at so i can add special formatting to that cell. On the first load, it will load appropriately and correctly, but soon as i begin moving up and down the view, the format doesn't stay and incorrect cells get the special formatting, this only happens to cells outside my view. Im not sure why this happening, but i'm fairly certain it is related to excessive refreshing.
import UIKit
class CalendarController: UIViewController, UITableViewDataSource, UITableViewDelegate, NSXMLParserDelegate {
var parser: NSXMLParser = NSXMLParser()
var info: [newsarticle] = []
var postTitle: String = String()
var postDesc: String = String()
var eName: String = String()
var index: Int = Int()
var organizedinfo: [String] = [String]()
var headDates: String = String()
var mapper: [Int] = [Int]()
var datemapper: [Int] = [Int]()
var colorcatch = 0
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
}
*/
@IBOutlet var multiview: UITableView!
var refreshControl: UIRefreshControl!
override func viewDidLoad() {
let url:NSURL = NSURL(string: "http://brrsd.k12.nj.us/rss/Calendar.xml")!
parser = NSXMLParser(contentsOfURL: url)!
parser.delegate = self
parser.parse()
orgData()
refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(WebpageController.refresh(_:)), forControlEvents: UIControlEvents.ValueChanged)
multiview.addSubview(refreshControl)
}
func retuDate(numb: String) -> String
{
let fullString = numb
let dates = fullString.characters.split{$0 == "-"}.map(String.init)
if(dates.count == 3)
{
retu dates[2]
}
else{
retu dates[1]
}
}
func orgData(){
var i = 0
while i < info.count{
if(retuDate(info[i].title) == headDates)
{
mapper.append(organizedinfo.count)
organizedinfo.append("*" + cleanTitle(info[i]))
}
else{
datemapper.append(organizedinfo.count)
organizedinfo.append("[" + retuDate(info[i].title).substringFromIndex(info[i].title.startIndex) + "]")
mapper.append(organizedinfo.count)
organizedinfo.append("*" + cleanTitle(info[i]))
headDates = retuDate(info[i].title)
}
i += 1
}
}
func cleanTitle(a: newsarticle) -> String{
let fullString = a.title
let dates = fullString.characters.split{$0 == "-"}.map(String.init)
if(dates.count == 2){
retu dates[0]
}
else{
retu dates[0] + dates[1]
}
}
func refresh(sender: AnyObject)
{
colorcatch = 0
let url:NSURL = NSURL(string: "http://brrsd.k12.nj.us/rss/Calendar.xml")!
parser = NSXMLParser(contentsOfURL: url)!
parser.delegate = self
parser.parse()
multiview.reloadData()
refreshControl.endRefreshing()
}
func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI namespaceURI: String?, qualifiedName qualifiedName: String?, attributes attributeDict: [String : String])
{
eName = elementName
if elementName == "item" {
postTitle = String()
postDesc = String()
}
}
override func didReceiveMemoryWaing() {
super.didReceiveMemoryWaing()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #waing Incomplete implementation, retu the number of sections
retu 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #waing Incomplete implementation, retu the number of rows
retu organizedinfo.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell
if let reusedCell = tableView.dequeueReusableCellWithIdentifier("Cell") {
cell = reusedCell
} else {
cell = UITableViewCell(style: .Default, reuseIdentifier: "Cell")
}
if let label = cell.textLabel {
label.text = organizedinfo[indexPath.row]
}
if(colorcatch < datemapper.count){
for element in datemapper{
if element == indexPath.row
{
cell.backgroundColor = UIColor.lightGrayColor()
cell.textLabel?.textColor = UIColor.blueColor()
}
}
colorcatch += 1
}
retu cell }
func parser(parser: NSXMLParser, foundCharacters string: String) {
let data = string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
if (!data.isEmpty) {
if eName == "title" {
postTitle += data
} else if eName == "description" {
postDesc += data
}
}
}
func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
if elementName == "item" {
let newsart: newsarticle = newsarticle()
newsart.title = postTitle
newsart.description = postDesc
info.append(newsart)
}
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
index = indexPath.row
var a = true
for element in datemapper{
if element == index
{
a = false
}
}
if a{
performSegueWithIdentifier("CalendarTransfer", sender:self)
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let Destination: FullCalendar = segue.destinationViewController as! FullCalendar
var i = 0
while mapper[i] != index{
i += 1
}
Destination.info = info[i]
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
