转换为Swift 3和Xcode 8后,我的应用程序中的UITableViews在单击单元格时都不会产生动作。
它仍然从数据源检索数据但不会产生任何警报或segue。
任何人都知道为什么会这样?
我通过转换回swift 2然后返回到Xcode 7.然而,它仍然无效。
*在故事板中设置代理和数据源
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
}
}
答案 0 :(得分:2)
这是 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 {
}
)
答案 1 :(得分:1)
您需要将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
}
}