我有一個視圖控制器,其中有一個表格視圖。我有一個類ProductCell.swift和控制器ProductVC.swift。我需要的是,當有人點擊tableCell中的用戶名按鈕時,它應該呈現配置文件視圖控制器。 here is the error image link . the error in description is : unexpectedily found nil while unwrapping an optional 我曾嘗試:如何從表格內的按鈕呈現視圖控制器
self.window?.rootViewController!.presentViewController(vc, animated: true, completion: nil)
我也打過電話委託。
上述兩種解決方案都給出相同的錯誤代碼。
顯示在數據源的tableview和委託=自該錯誤,當我從viewDidLoad中()除去這些話,就說明在self.tableview.reloadData()
我看到3,4問題錯誤同樣的問題,但沒有解決方案爲我工作。 如果問題不明確,請讓我知道。我會再解釋一遍。
我被困在這個從幾個小時。
UPDATING碼
FOR CatPlaceTableViewCell
import UIKit
protocol TableViewCellDelegate
{
func buttonDidClicked()
}
class CatPlaceTableViewCell: UITableViewCell{
var delegate: TableViewCellDelegate?
@IBOutlet weak var sponsoredView: UIView!
@IBOutlet weak var catProfilePic: UIImageView!
@IBOutlet weak var catProfileName: UIButton!
@IBAction func catProfileNameBtnPressed(_ sender: Any) {
self.delegate?.buttonDidClicked()
}
@IBOutlet weak var catPostTime: UILabel!
@IBOutlet weak var catPostImage: UIImageView!
@IBOutlet weak var catPostTitle: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
func configureCell(post : PlacesDetails)
{
catPostTitle.text = post.title
catProfileName.setTitle(post.fullName, for: .normal)
catPostTime.text = post.postTime
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
FOR PlaceCategoryVC:
import UIKit
import Alamofire
import SDWebImage
class PlaceCategoryVC: UIViewController , UITableViewDataSource , UITableViewDelegate , TableViewCellDelegate{
@IBOutlet weak var headerPanel: UIView!
@IBOutlet weak var searchLocation: UISearchBar!
@IBOutlet weak var applyFIlter: UIButton!
@IBOutlet weak var resetFilter: UIButton!
@IBAction func applyFilterBtnPressed(_ sender: Any) {
}
@IBAction func resetFilterBtnPressed(_ sender: Any) {
}
////////////////////////////
@IBOutlet weak var catPostTable: UITableView!
typealias DownloadComplete1 =() ->()
var post : PlacesDetails!
var posts = [PlacesDetails]()
override func viewDidLoad() {
super.viewDidLoad()
catPostTable.dataSource = self
catPostTable.delegate = self
downloadPostData {}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : CatPlaceTableViewCell = catPostTable.dequeueReusableCell(withIdentifier: "catPlaces", for: indexPath) as! CatPlaceTableViewCell
// cell.delegate = self
if(indexPath.row % 5 == 4)
{
cell.sponsoredView.isHidden = false
}
else
{
cell.sponsoredView.isHidden = true
}
cell.catProfilePic.sd_setImage(with: URL(string: posts[indexPath.row].profileImageURL), placeholderImage: UIImage(named: "default_image.png"))
cell.catPostImage.sd_setImage(with: URL(string: posts[indexPath.row].contentURL), placeholderImage: UIImage(named: "placeholder.png"))
let post1 = posts[indexPath.row]
cell.configureCell(post: post1)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("\(posts[indexPath.row].postid)")
let indexpath1 = posts[indexPath.row].postid
UserDefaults.standard.setValue(indexpath1, forKey: "postIdOfSinglePOst")
let viewController:UIViewController = self.storyboard!.instantiateViewController(withIdentifier: "SinglePostSB") as UIViewController
self.present(viewController, animated: true, completion: nil)
}
func downloadPostData(completed: @escaping DownloadComplete1) {
var i = 1
repeat {
Alamofire.request("https://www.EXAMPLE.COM?category=places&page=\(i)").responseJSON { response in
let result = response.result
print(result)
if let dict = result.value as? Dictionary<String,AnyObject> {
if let successcode = dict["STATUS_CODE"] as? Int {
if successcode == 1 {
if let postsArray = dict["posts"] as? [Dictionary<String,AnyObject>]
{
for obj in postsArray
{
let post = PlacesDetails(postDict: obj)
self.posts.append(post)
print(self.posts)
}
self.catPostTable.reloadData()
}
}
}
}
}
i = i + 1
}
while(i<2)
completed()
}
var delegate: TableViewCellDelegate?
func buttonDidClicked()
{
let vc = ProductVC()
self.present(vc, animated: true, completion: nil)
}
}
你能顯示更多的代碼 –
好吧,等待我更新代碼 –
你也可以在這裏打印你的錯誤信息 – Aximem