我如何从UITableView更改为UIViewController

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我如何从UITableView更改为UIViewController相关的知识,希望对你有一定的参考价值。

我想调整我的代码到UIViewController,所以我可以实际控制事物并实现导航栏,就像我在我的应用程序的其他部分一样。

这是当前的代码。

import UIKit
import SafariServices
import TIFeedParser
import Alamofire

class Listen: UITableViewController {
@IBOutlet weak var MyMenu: UIBarButtonItem!


var items : Array<Item> = []
var entries : Array<Entry> = []

override func viewDidLoad() {
    super.viewDidLoad()

    loadRSS()
    loadAtom()

    MYMenu.target = self.revealViewController()
    MYMenu.action = #selector(SWRevealViewController.revealToggle(_:))
    self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}


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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath)

    let item:Item = self.items[indexPath.row]
    cell.textLabel?.text = item.title
    cell.detailTextLabel?.text = self.pubDateStringFromDate(item.pubDate! as Date)

    return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let item = self.items[indexPath.row]

    let url:URL = URL(string: item.link!)!
    let safariViewController = SFSafariViewController(url: url, entersReaderIfAvailable: true)
    present(safariViewController, animated: true, completion: nil)
}

func loadRSS() {

    let feedUrlString:String = "https://vimeo.com/MyChannel/videos/rss"

    Alamofire.request(feedUrlString).response { response in

        if let data = response.data, let _ = String(data: data, encoding: .utf8) {

            TIFeedParser.parseRSS(xmlData: data as NSData, completionHandler: {(isSuccess, channel, error) -> Void in

                if (isSuccess) {
                    self.items = channel!.items!
                    self.tableView.reloadData()
                }

                if (response.error != nil) {
                    print((response.error?.localizedDescription)! as String)
                }
            })
        }
    }

}

func loadAtom() {

    let feedUrlString:String = "https://vimeo.com/MyChannel/videos/rss"

    Alamofire.request(feedUrlString).response { response in

        if let data = response.data, let _ = String(data: data, encoding: .utf8) {

            TIFeedParser.parseAtom(xmlData: data as NSData, completionHandler: {(isSuccess, feed, error) -> Void in

                if (isSuccess) {
                    self.entries = feed!.entries!
                    self.tableView.reloadData()
                }

                if (error != nil) {
                    print((error?.localizedDescription)! as String)
                }
            })
        }
    }
}

func pubDateStringFromDate(_ pubDate:Date)->String {
    let format = DateFormatter()
    format.dateFormat = "yyyy/M/d HH:mm"

    let pubDateString = format.string(from: pubDate)
    return pubDateString
}

override var prefersStatusBarHidden: Bool {
    return true
}


}

我想基本上将TableView作为Subview ...当我尝试将此代码改编为UIViewController时,我得到了多个错误,我尝试了从使用Lets到创建私人操作的所有内容,甚至使用objective-c作为解决方法但仍然无法加载。

有没有一种简单的方法来实现这一目标?

我的主要目标是让导航栏浮动在UITableView上方。

class Listen: UIViewController {
@IBOutlet weak var MyMenu: UIBarButtonItem!
@IBOutlet weak var tableView: UITableView!


var items : Array<Item> = []
var entries : Array<Entry> = []

override func viewDidLoad() {
    super.viewDidLoad()

    loadRSS()
    loadAtom()

    MyMenu.target = self.revealViewController()
    MyMenu.action = #selector(SWRevealViewController.revealToggle(_:))
    self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}


 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return items.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath)

    let item:Item = self.items[indexPath.row]
    cell.textLabel?.text = item.title
    cell.detailTextLabel?.text = self.pubDateStringFromDate(item.pubDate! as Date)

    return cell
}

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let item = self.items[indexPath.row]

    let url:URL = URL(string: item.link!)!
    let safariViewController = SFSafariViewController(url: url, entersReaderIfAvailable: true)
    present(safariViewController, animated: true, completion: nil)
}

func loadRSS() {

    let feedUrlString:String = "https://vimeo.com/MyChannel/videos/rss"

    Alamofire.request(feedUrlString).response { response in

        if let data = response.data, let _ = String(data: data, encoding: .utf8) {

            TIFeedParser.parseRSS(xmlData: data as NSData, completionHandler: {(isSuccess, channel, error) -> Void in

                if (isSuccess) {
                    self.items = channel!.items!
                    self.tableView.reloadData()
                }

                if (response.error != nil) {
                    print((response.error?.localizedDescription)! as String)
                }
            })
        }
    }

}

func loadAtom() {

    let feedUrlString:String = "https://vimeo.com/MyChannel/videos/rss"

    Alamofire.request(feedUrlString).response { response in

        if let data = response.data, let _ = String(data: data, encoding: .utf8) {

            TIFeedParser.parseAtom(xmlData: data as NSData, completionHandler: {(isSuccess, feed, error) -> Void in

                if (isSuccess) {
                    self.entries = feed!.entries!
                    self.tableView.reloadData()
                }

                if (error != nil) {
                    print((error?.localizedDescription)! as String)
                }
            })
        }
    }
}

func pubDateStringFromDate(_ pubDate:Date)->String {
    let format = DateFormatter()
    format.dateFormat = "yyyy/M/d HH:mm"

    let pubDateString = format.string(from: pubDate)
    return pubDateString
}

override var prefersStatusBarHidden: Bool {
    return true
}


}

谁能告诉我为什么上面的代码无法控制子视图?

Old Way New Way

答案

好的,你的问题很混乱,

如果您想让您的表位于导航栏下方,则无需将UITableViewController转换为UIViewController。 1 - 使用导航控制器并将其连接到视图控制器,2 - 在故事板上选择导航控制器>在右栏上单击“显示属性检查器”,在Simulated Metrics中将顶部栏更改为Opaque navigation bar,这将使下面的嵌套视图酒吧。

但是,如果您只想修复问题并修复代码中的构建问题,请尝试以下操作:

class SWRevealViewController:UIViewController {
    var panGestureRecognizer:UIPanGestureRecognizer?
    override func viewDidLoad() {
        panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(onPanned))
    }
    @objc
    func onPanned(sender:UIPanGestureRecognizer){

    }
    @objc
    func revealToggle(sender:Any) {

    }
}
class Listen: UIViewController {
    @IBOutlet weak var MyMenu: UIBarButtonItem!
    @IBOutlet weak var tableView: UITableView!
    var revealViewController:SWRevealViewController?

    var items : Array<Item> = []
    var entries : Array<Entry> = []

    override func viewDidLoad() {
        super.viewDidLoad()

        loadRSS()
        loadAtom()

       guard let revealViewController = self.revealViewController, let panGestureRecognizer = revealViewController.panGestureRecognizer else {
        return
        }
        MyMenu.target = self.revealViewController
        MyMenu.action = #selector(revealViewController.revealToggle)
        self.view.addGestureRecognizer(panGestureRecognizer)


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath)

        let item:Item = self.items[indexPath.row]
        cell.textLabel?.text = item.title
        cell.detailTextLabel?.text = self.pubDateStringFromDate(item.pubDate! as Date)

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let item = self.items[indexPath.row]

        let url:URL = URL(string: item.link!)!
        let safariViewController = SFSafariViewController(url: url, entersReaderIfAvailable: true)
        present(safariViewController, animated: true, completion: nil)
    }

    func loadRSS() {

        let feedUrlString:String = "https://vimeo.com/MyChannel/videos/rss"

        Alamofire.request(feedUrlString).response { response in

            if let data = response.data, let _ = String(data: data, encoding: .utf8) {

                TIFeedParser.parseRSS(xmlData: data as NSData, completionHandler: {(isSuccess, channel, error) -> Void in

                    if (isSuccess) {
                        self.items = channel!.items!
                        self.tableView.reloadData()
                    }

                    if (response.error != nil) {
                        print((response.error?.localizedDescription)! as String)
                    }
                })
            }
        }

    }

    func loadAtom() {

        let feedUrlString:String = "https://vimeo.com/MyChannel/videos/rss"

        Alamofire.request(feedUrlString).response { response in

            if let data = response.data, let _ = String(data: data, encoding: .utf8) {

                TIFeedParser.parseAtom(xmlData: data as NSData, completionHandler: {(isSuccess, feed, error) -> Void in

                    if (isSuccess) {
                        self.entries = feed!.entries!
                        self.tableView.reloadData()
                    }

                    if (error != nil) {
                        print((error?.localizedDescription)! as String)
                    }
                })
            }
        }
    }

    func pubDateStringFromDate(_ pubDate:Date)->String {
        let format = DateFormatter()
        format.dateFormat = "yyyy/M/d HH:mm"

        let pubDateString = format.string(from: pubDate)
        return pubDateString
    }

    override var prefersStatusBarHidden: Bool {
        return true
    }


}

以上是关于我如何从UITableView更改为UIViewController的主要内容,如果未能解决你的问题,请参考以下文章

如何将 UITableView 高度更改为其内容高度并且仍然具有可点击的内容?

将 UITableView 的标题更改为 iOS 中的单元格名称

如果宽度和高度不同,如何在 UITableView 中将 UITableViewCell Image 更改为 Circle

从字典数组 Swift 中填充 UITableView

如何以编程方式截取可扩展的uitableview的屏幕截图

iOS 7 将 UIView 更改为 UITableView