2017-08-14 80 views
0

我想解析表格視圖單元格中的json數據,並且不解析並且不顯示單元格。我附上我所有的代碼,請告訴我最新的錯誤是什麼..我沒有收到任何錯誤,但單元格沒有顯示。我已經爲json解析並將其存儲在一個數組中而創建了單獨的類和函數。在表格視圖單元格中通過alamofire解析json數據

// 
// ViewController.swift 
// WorkingFeed2 
// 
// Created by keshu rai on 08/08/17. 
// Copyright © 2017 keshu rai. All rights reserved. 
// 

import UIKit 
import Alamofire 
import MediaPlayer 

class ViewController: UIViewController , UITableViewDelegate , UITableViewDataSource{ 

var post : PostData! 
var posts = [PostData]() 
typealias DownloadComplete =() ->() 

var arrayOfPostData : [String] = [] 
@IBOutlet weak var feedTable: UITableView! 
override func viewDidLoad() { 
    super.viewDidLoad() 

    feedTable.dataSource = self 
    feedTable.delegate = self 


} 

func downloadPostData(completed: @escaping DownloadComplete) { 
    Alamofire.request("https://new.example.com/api/posts/get_all_posts").responseJSON { response in 
    let result = response.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 = PostData(postDict: obj) 
         self.posts.append(post) 
         print(obj) 
        } 
//      self.posts.remove(at: 0) 
        self.feedTable.reloadData() 
       } 
      } 
     } 
    } 
    } 
} 

func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return posts.count 
} 
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    return 419 
} 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell : ImageTableViewCell = self.feedTable.dequeueReusableCell(withIdentifier: "contentViewReuse") as! ImageTableViewCell 
    let post = posts[indexPath.row] 
    print(post) 
    cell.configureCell(post : post) 
    return cell 


} 
} 

這是我的PostData類。

import Foundation 

class PostData { 
var _profileImageURL : String? 
var _fullName : String? 
var _location : String? 
var _title : String? 
var _postTime : String? 
var _likes : Int? 
var _comments : Int? 
var _mediaType : String? 
var _contentURL : String? 
var _content : String? 
var _plocation : String? 

var profileImageURL : String 
{ 
    if _profileImageURL == nil { 
     _profileImageURL = "" 
    } 
    return _profileImageURL! 
} 

var fullName : String 
{ 
    if _fullName == nil { 
     _fullName = "" 
    } 
    return _fullName! 
} 
var location : String { 
    if _location == nil { 
     _location = "" 
    } 
    return _location! 
} 
var title : String { 
    if _title == nil { 
     _title = "" 
    } 
    return _title! 
} 

var postTime : String { 
    if _postTime == nil { 
     _postTime = "" 
    } 
    return _postTime! 
} 

var likes : Int { 
    if _likes == nil { 
     _likes = 0 
    } 
    return _likes! 
} 

var comments : Int { 
    if _comments == nil { 
     _comments = 0 
    } 
    return _comments! 
} 

var mediaType : String { 
    if _mediaType == nil { 
     _mediaType = "" 
    } 
    return _mediaType! 
} 

var contentURL : String { 
    if _contentURL == nil { 
     _contentURL = "" 
    } 
    return _contentURL! 
} 
var content : String { 
    if _content == nil { 
     _content = "" 
    } 
    return _content! 
} 
var pLocation : String { 
    if _plocation == nil { 
     _plocation = "" 
    } 
    return _plocation! 
} 






init(postDict : Dictionary<String , AnyObject>) 
{ 
    if let postsArray = postDict["posts"] as? [Dictionary<String,AnyObject>] 
    { 
     for i in 1..<postsArray.count 
     { 

      let fullName1 = postsArray[i]["full_name"] as? String 
      self._fullName = fullName1 


      let profileImageURL1 = postsArray[i]["profile_pic"] as? String 
      self._profileImageURL = profileImageURL1 

      let location1 = postsArray[i]["user_city"] as? String 
      self._location = location1 

      let title1 = postsArray[i]["title"] as? String 
      self._title = title1 

      let postTime1 = postsArray[i]["order_by_date"] as? String 
      self._postTime = postTime1 

      let likes1 = postsArray[i]["liked_count"] as? Int 
      self._likes = likes1 

      let comments1 = postsArray[i]["comment_count"] as? Int 
      self._comments = comments1 

      let mediaType1 = postsArray[i]["media_path"] as? String 
      self._mediaType = mediaType1 

      let contentURL1 = postsArray[i]["media_path"] as? String 
      self._contentURL = contentURL1 

      let content1 = postsArray[i]["content"] as? String 
      self._content = content1 

      let plocation1 = postsArray[i]["location"] as? String 
      self._plocation = plocation1 
     } 
    } 
} 
} 

這是我的PostDataTableViewCell代碼。

import UIKit 
import Alamofire 

class PostDataTableViewCell: UITableViewCell { 



@IBOutlet weak var profileImage: UIImageView! 
@IBOutlet weak var titlePost: UILabel! 
@IBOutlet weak var profileFullName: UILabel! 
@IBOutlet weak var profileUserLocation: UILabel! 
@IBOutlet weak var likeBtn: UIButton! 
var buttonAction: (() -> Void) = {} 


var pressed = false 
@IBAction func likeBtnPressed(_ sender: Any) { 
    if !pressed { 
     let image = UIImage(named: "Like-1.png") as UIImage! 
     likeBtn.setImage(image, for: .normal) 
     pressed = true 
    } else { 

     let image = UIImage(named: "liked.png") as UIImage! 
     likeBtn.transform = CGAffineTransform(scaleX: 0.15, y: 0.15) 

     UIView.animate(withDuration: 2.0, 
         delay: 0, 
         usingSpringWithDamping: 0.2, 
         initialSpringVelocity: 6.0, 
         options: .allowUserInteraction, 
         animations: { [weak self] in 
         self?.likeBtn.transform = .identity 
      }, 
         completion: nil) 
     likeBtn.setImage(image, for: .normal) 
     pressed = false 
    } 


} 
@IBAction func commentBtnPressed(_ sender: Any) { 
    print("Commented") 

} 
@IBAction func shareBtnPressed(_ sender: Any) { 
    self.buttonAction() 


} 
@IBAction func readContentBtnPressed(_ sender: Any) { 
    print("Read") 

} 

@IBOutlet weak var contentPostLabel: UILabel! 

@IBOutlet weak var contentTypeView: UIView! 
@IBOutlet weak var likeAndCommentView: UIView! 
@IBOutlet weak var numberOfLikes: UILabel! 
@IBOutlet weak var numberOfComments: UILabel! 
@IBOutlet weak var postLocation: UILabel! 
@IBOutlet weak var postTimeOutlet: UILabel! 


func configureCell(post : PostData) 
{ 
    titlePost.text = "\(post.title)" 
    profileFullName.text = "\(post.fullName)" 
    profileUserLocation.text = "\(post.location)" 
    numberOfLikes.text = "\(post.likes) Likes" 
    numberOfComments.text = "\(post.comments) Comments" 
    postLocation.text = "\(post.pLocation)" 
    postTimeOutlet.text = "\(post.postTime)" 
     let url = URL(string: post.profileImageURL) 
     let data = try? Data(contentsOf: url!) 
    profileImage.image = UIImage(data: data!) 
    contentPostLabel.text = "\(post.content)" 

    if post.mediaType == "image" 
    { 
    let url1 = URL(string: post.contentURL) 
    let data1 = try? Data(contentsOf: url1!) 
    let image = UIImage(data: data1!) 
    let imageToView = UIImageView(image: image!) 
    imageToView.frame = CGRect(x: 0, y: 0, width: 375 , height: 250) 
    imageToView.contentMode = UIViewContentMode.scaleToFill 
    contentTypeView.addSubview(imageToView) 


    } 
    else if post.mediaType == "null" 
    { 
     print("Status") 
    } 
    else if post.mediaType == "video" 
    { 
     print("Video") 
    } 
    else if post.mediaType == "youtube" 
    { 
    print("youtube") 
    } 
} 

} 
+1

除了對*假裝*是常數是斯威夫特可笑。只需使用'let'關鍵字即可。 – vadian

+0

@keshu Rai打電話去表內功能?那時你有數據嗎?使用斷點並調試您的代碼。 –

+0

實際上沒有..價值沒有得到存儲在數組..我打電話給陣列中的viewdid加載打印功能,它顯示一個空的錯誤..你可以請通過代碼,讓我知道我在做什麼錯誤。 。請大家多多幫助,因爲我很久以前就被這個代碼困住了。 –

回答

0

最有可能出現問題,因爲你是在PostDataViewController試圖解析關鍵posts兩次值,一次一次。

首先在Swift 3中,一個JSON字典是[String:Any],其次 - 正如我的評論中已經提到的那樣 - 私人支持變量在Swift中是無稽之談。

PostData可以降低到

class PostData { 
    let profileImageURL : String 
    let fullName : String 
    let location : String 
    let title : String 
    let postTime : String 
    let likes : Int 
    let comments : Int 
    let mediaType : String 
    let contentURL : String 
    let content : String 
    let plocation : String 


    init(postDict : [String:Any]) 
    {   
     fullName = postDict["full_name"] as? String ?? "" 
     profileImageURL = postDict["profile_pic"] as? String ?? "" 
     location = postDict["user_city"] as? String ?? "" 
     title = postDict["title"] as? String ?? "" 
     postTime = postDict["order_by_date"] as? String ?? "" 
     likes = postDict["liked_count"] as? Int ?? 0 
     comments = postDict["comment_count"] as? Int ?? 0 
     mediaType = postDict["media_path"] as? String ?? "" 
     contentURL = postDict["media_path"] as? String ?? "" 
     content = postDict["content"] as? String ?? "" 
     plocation = postDict["location"] as? String ?? "" 
    } 
} 
從問題私人後盾變量
+0

老兄,這是有益的..現在我得到的數據在我的細胞非常感謝......但我沒有得到我已經通過視圖內的圖像。在我的PostDataTableViewCell代碼中的configureCell()函數中。任何解決方案? –

+0

基本上同步加載數據是不好的。您應該在模型中添加圖像屬性並使用緩存系統。在您的代碼中,當用戶滾動時,圖像會一次又一次地重新加載。並且不要對字符串使用字符串插值'「\()」'。這是多餘的。 – vadian

+0

actully m只是一個新鮮..米在學習階段.. wud肯定改善我的代碼:)謝謝 –

相關問題