升级到Swift 3后UITableView坏了

巴布兹77

在转换为Swift 3和Xcode 8之后,我的应用程序中的两个UITableViews在单击单元格时都不会产生动作。
它仍然从数据源中检索数据,但不会产生任何警报或警告。

有人知道为什么会这样吗?

我回到转换为swift 2,然后回到Xcode7。
但是,它仍然不起作用。

*在情节提要中设置的委托和数据源

import UIKit


protocol TravelFeedViewControllerDelegate {
    func controllerDidStartLoading(controller: TravelFeedViewController)
    func controllerDidStopLoading(controller: TravelFeedViewController)
}

class TravelFeedViewController: UIViewController {

    // MARK: - Properties

    @IBOutlet weak var tableView: UITableView!

    enum FeedMode: String {
        case Global
        case Personal
    }

    var feedMode: FeedMode!                                 // Setting the mode to global/personal will fetch the corresponding array
    var delegate: TravelFeedViewControllerDelegate?
    var tableDataSource = [String]()                //[Post]()
    let cellIdentifier = "FeedCell"

    // MARK: - View Life Cycle

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.layer.cornerRadius = 5.0
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        assert(feedMode != nil, "Set feedMode before presenting controller")

        self.loadFeed()
    }

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

    // MARK: - Posts

    /**
     Requests posts
     */
    func loadFeed() {
        self.delegate?.controllerDidStartLoading(self)

        if self.feedMode == .Global {
            self.tableDataSource = ["Global","Global","Global","Global","Global","Global","Global"]
//            User.getGlobalPosts { (syttrs: Array<Post>?) -> Void in
//                if let syttrsArray = syttrs {
//                    self.tableDataSource = syttrsArray
//                    self.tableView.reloadData()
//                }
//                self.delegate?.controllerDidStopLoading(self)
//            }

            // Load parents
        } else if self.feedMode == .Personal {
            self.tableDataSource = ["Personal"]
//            User.getFriendsPosts({ (parents: Array<Post>?) -> Void in
//                if let parentArray = parents {
//                    self.tableDataSource = parentArray
//                    self.tableView.reloadData()
//                }
//                self.delegate?.controllerDidStopLoading(self)
//            })
        }
        self.tableView.reloadData()
        self.delegate?.controllerDidStopLoading(self)
    }
}

extension TravelFeedViewController: UITableViewDelegate, UITableViewDataSource {

    func tableView(tableView: UITableView, estimatedHeightForRowAt indexPath: NSIndexPath) -> CGFloat {
        return 90.0
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return  1
    }

    func tableView(tableView: UITableView, didSelectRowAt indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        let post = self.tableDataSource[(indexPath as NSIndexPath).row]

        if let profileVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("PostDetailVC") as? PostDetailViewController {
            profileVC.post = post
            self.navigationController?.pushViewController(profileVC, animated: true)
        }
    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
        if let castedCell = cell as? FeedTableViewCell {
            let post = self.tableDataSource[(indexPath as NSIndexPath).row]
            castedCell.postLabel.text = post
            castedCell.imageContainer.layer.cornerRadius = castedCell.imageContainer.frame.size.width / 2.0
            castedCell.imageContainer.backgroundColor = Constants.Colors.QIconGray
            castedCell.personImageView.image = nil
            castedCell.personImageView.image = UIImage(named: "silhouette") // = UIImage(data: result!)
        }

        return cell
    }
}
大卫·希克

您需要将tableView代码更改为Swift 3:

extension TravelFeedViewController: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return 90.0
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return  1
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        let post = self.tableDataSource[(indexPath as NSIndexPath).row]

        if let profileVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("PostDetailVC") as? PostDetailViewController {
            profileVC.post = post
            self.navigationController?.pushViewController(profileVC, animated: true)
        }
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
        if let castedCell = cell as? FeedTableViewCell {
            let post = self.tableDataSource[(indexPath as NSIndexPath).row]
            castedCell.postLabel.text = post
            castedCell.imageContainer.layer.cornerRadius = castedCell.imageContainer.frame.size.width / 2.0
            castedCell.imageContainer.backgroundColor = Constants.Colors.QIconGray
            castedCell.personImageView.image = nil
            castedCell.personImageView.image = UIImage(named: "silhouette") // = UIImage(data: result!)
        }

        return cell
    }

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

升级到Swift 1.2后的错误

来自分类Dev

升级到Swift 1.2后的错误

来自分类Dev

升级到Swift 2.1后,Swift返回optional(“ value”)

来自分类Dev

升级到Swift 1.2后SpriteKit的问题

来自分类Dev

升级到Swift 3.0后网址为零

来自分类Dev

升级到Swift 3时使用未声明的类型

来自分类Dev

升级到OS X Mavericks后Drush坏了

来自分类Dev

升级到14.04后,Chrome和Skype损坏了

来自分类Dev

升级到 Ubuntu 20.04 后双引导坏了

来自分类Dev

从 18.04 升级到 20.04 后 Pi 孔坏了

来自分类Dev

从Swift 2.2升级到Swift 3.0后,继续使用`continue`

来自分类Dev

升级到El-Capitan后的Swift编译器错误

来自分类Dev

升级到Symfony 2.8后的MappingException

来自分类Dev

登录后升级到13.10,黑屏

来自分类Dev

升级到13.10后的登录循环

来自分类Dev

升级到9.5.17后的安全提示

来自分类Dev

升级到0.60.5后的ReactWebViewManager错误

来自分类Dev

登录后升级到13.10,黑屏

来自分类Dev

升级到13.10后的登录循环

来自分类Dev

升级到13.10后登录无处

来自分类Dev

升级到17.10后无法上网

来自分类Dev

升级到14.10后桌面冻结

来自分类Dev

升级到19.04后的登录循环

来自分类Dev

升级到15.10后ZFS损坏

来自分类Dev

升级到16.04后libgtk的问题

来自分类Dev

升级到Symfony 2.8后的MappingException

来自分类Dev

升级到5.2后出现错误

来自分类Dev

升级到17.04后无法上网

来自分类Dev

升级到Ubuntu 18.04后的Bootloop