Transition 后 TableViewController 几秒不出现

TableViewController does not appear for a few seconds after Transition

我有一个带有四个 tableviewcontroller 的 tabbarcontroller,它们由导航控制器连接。 table 视图由 XMLParser 从 Internet 下载的图像和文本填充。当应用程序加载时,启动画面后,屏幕变黑几秒钟,然后出现第一个 table 视图。在其他 table 视图上的 Tab 键点击也有延迟。当 tableview controller 的数据被下载时,我如何显示一些东西来代替黑屏或无响应的界面?

table视图之一的代码:

import UIKit

class TopicsTableViewController: UITableViewController, XMLParserDelegate {

var xmlParser : XMLParser!

override func viewDidLoad() {
    super.viewDidLoad()

    let url = NSURL(string: "http://sharontalon.com/feed")
    xmlParser = XMLParser()
    xmlParser.delegate = self
    xmlParser.startParsingWithContentsOfURL(url!)
}

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


// MARK: XMLParserDelegate method implementation

func parsingWasFinished() {
    self.tableView.reloadData()
}


// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return xmlParser.arrParsedData.count
}


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


    let currentDictionary = xmlParser.arrParsedData[indexPath.row] as Dictionary<String, String>
    let url = currentDictionary["enclosure"]
    let data = NSData(contentsOfURL: url!.asNSURL) //make sure your image in this url does exist, otherwise unwrap in a if let check

    let description = currentDictionary["description"]


    cell.selectionStyle = UITableViewCellSelectionStyle.None
    cell.textLabel?.text = currentDictionary["title"]
    cell.detailTextLabel?.text = String(htmlEncodedString: description!)
            cell.detailTextLabel?.numberOfLines = 3;
    cell.textLabel?.numberOfLines = 2;

        cell.imageView?.image = UIImage(data: data!)


    return cell
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 80
}


override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let dictionary = xmlParser.arrParsedData[indexPath.row] as Dictionary<String, String>
    let tutorialLink = dictionary["link"]
    let publishDate = dictionary["pubDate"]

    let tutorialViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("idTutorialViewController") as! TutorialViewController

    tutorialViewController.tutorialURL = NSURL(string: tutorialLink!)
    tutorialViewController.publishDate = publishDate

    showDetailViewController(tutorialViewController, sender: self)

}

这可能是由一个简单的线程问题引起的,请试一试,如果它不起作用,我会尽力帮助您:

首先将资源繁重的操作移至后台线程:

override func viewDidLoad() {
    super.viewDidLoad()

     let url = NSURL(string: "http://sharontalon.com/feed")
     xmlParser = XMLParser()
     xmlParser.delegate = self

    dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), {
         self.xmlParser.startParsingWithContentsOfURL(url!)
    })
}

接下来,将更新用户界面的所有代码移动到前台线程:

func parsingWasFinished() {
    dispatch_async(dispatch_get_main_queue(), {
        self.tableView.reloadData()
    })
}

如果这不能解决您的问题,请告诉我我会删除此答案并重新考虑您的问题。

重新加载 table 数据必须在 table 的主线程上才能立即更新。

func parsingWasFinished() {
    self.tableView.reloadData()
}

将是:

func parsingWasFinished() {
    dispatch_async(dispatch_get_main_queue(), {
        self.tableView.reloadData()
    })
}