UITableView在没有命令的情况下过度刷新

时间:2016-06-15 22:58:55

标签: ios swift uitableview

我在UIViewController中有一个表视图,以表格格式显示日历信息。它显示的信息包含一行专用于日期,然后是当天所有事件,等等。我添加了一个int的mapper数组,所以我知道每个日期所在的行索引,所以我可以为该单元格添加特殊格式。在第一次加载时,它将正确加载并正确加载,但是当我开始上下移动视图时,格式不会停留,不正确的单元格会获得特殊格式,这只发生在我视图之外的单元格中。我不确定为什么会这样,但我相当肯定它与过度清新有关。

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 returnDate(numb: String) -> String
    {
     let fullString = numb
    let dates = fullString.characters.split{$0 == "-"}.map(String.init)
      if(dates.count == 3)
      {
        return dates[2]
        }
        else{
            return dates[1]
        }

    }
    func orgData(){
        var i = 0
        while i < info.count{
            if(returnDate(info[i].title) == headDates)
            {
                mapper.append(organizedinfo.count)
                organizedinfo.append("*" + cleanTitle(info[i]))
            }
            else{
                datemapper.append(organizedinfo.count)
                organizedinfo.append("[" + returnDate(info[i].title).substringFromIndex(info[i].title.startIndex) + "]")
                mapper.append(organizedinfo.count)
                organizedinfo.append("*" + cleanTitle(info[i]))
                headDates = returnDate(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){
        return dates[0]
        }
        else{
            return 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 didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 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
        }
        return 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.
     }
     */

}

0 个答案:

没有答案