升级到swift 3后UITableView崩溃了



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

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

我回到swift 2,回到Xcode 7。
但是,它仍然不工作。

*委托和数据源设置在storyboard

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
    }
}

这是Swift 3的正确语法:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
}

您需要将您的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
    }
}

最新更新