2017-02-03 2 views
-1

죄송합니다. 내가 다른 뷰 컨트롤러에서 구현하고 싶은 두 번째 테이블 뷰에 대한 도움이 필요하다. 기본적으로이 코드는 기본적으로 실제로 작동하는 다른 tableView 컨트롤러에서 복사했습니다. 그래서 내가 어디서 엉망인지 모르겠지만 도움을 정말 감사 할 것입니다.Swift 3에서 jSON으로 TableView를 채우는 방법

class MCViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

    let message_url = "https://www.distribber.com/api/resources/get_film_message/film_id/3825" 
    let send_url = "https://www.distribber.com/api/resources/send_film_message" 
    let film_id = "3825" 
    var messageArray = [String]() 
    weak var messageView : UITableView! 

    func messageView(_ messageView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return messageArray.count 
    } 
    func messageView(_ messageView: UITableView, cellForRowAt indexPath: IndexPath) -> MessageTableViewCell { 
     let cell = messageView.dequeueReusableCell(withIdentifier: "msgContent", for:indexPath) as! MessageTableViewCell 
     // Configuring Cell 
     cell.msgContent.text = messageArray[indexPath.row] 
     // Returning the cell 
     return cell 
    } 



    @IBOutlet weak var MessageInput: UITextField! 
    @IBAction func Sendmsg(_ sender: Any) { 
     Sendmsg(username:MessageInput.text!, password: film_id) 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     messageView.dataSource = self 
     messageView.delegate = self 
     // Do any additional setup after loading the view. 
     //let post_data: NSDictionary = NSMutableDictionary() 


     //  post_data.setValue(username, forKey: "username") 
     //  post_data.setValue(password, forKey: "password") 

     let url:URL = URL(string: message_url)! 
     let session = URLSession.shared 

     let request = NSMutableURLRequest(url: url) 
     request.httpMethod = "GET" 
     request.setValue("740c94c51891c02b64d6c78840b478fe0b02fe2c", forHTTPHeaderField: "X-API-KEY") 
     request.setValue("Basic YmhlZW0uZW5nckBnbWFpbC5jb206YmgzM20=", forHTTPHeaderField: "Authorization") 
     request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData 
     // Do any additional setup after loading the view. 
     var paramString = "" 


     //  for (key, value) in post_data 
     //  { 
     //   paramString = paramString + (key as! String) + "=" + (value as! String) + "&" 
     //  } 
     // 
     request.httpBody = paramString.data(using: String.Encoding.utf8) 

     let task = session.dataTask(with: request as URLRequest, completionHandler: { 
      (
      data, response, error) in 

      guard let _:Data = data, let _:URLResponse = response , error == nil else { 

       return 
      } 



      let json: Any? 

      do 
      { 
       json = try JSONSerialization.jsonObject(with: data!, options: []) 
       if let parsedData = json as? [[String:Any]] { 
        for dict in parsedData { 
         if let title = dict["message"] as? String { 
          self.messageArray.append(title) 
          print(json) 

         } 
        } 
        OperationQueue.main.addOperation({ 
         self.messageView.reloadData() 
        }) 
       } 
      } 
      catch 
      { 
       return 
      } 

      guard let server_response = json as? NSDictionary else 
      { 
       return 
      } 


      if let data_block = server_response["data"] as? NSDictionary 
      { 
       if let session_data = data_block["session"] as? String 
       { 
        // self.login_session = session_data 

        let preferences = UserDefaults.standard 
        preferences.set(session_data, forKey: "session") 

        // DispatchQueue.main.async(execute: self.LoginDone) 
       } 
      } 



     }) 

     task.resume() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func Sendmsg(username:String, password:String) 
    { 
     let post_data: NSDictionary = NSMutableDictionary() 


     post_data.setValue(username, forKey: "message") 
     post_data.setValue(password, forKey: "film_id") 

     let url:URL = URL(string: send_url)! 
     let session = URLSession.shared 

     let request = NSMutableURLRequest(url: url) 
     request.httpMethod = "POST" 
     request.setValue("740c94c51891c02b64d6c78840b478fe0b02fe2c", forHTTPHeaderField: "X-API-KEY") 
     request.setValue("Basic YmhlZW0uZW5nckBnbWFpbC5jb206YmgzM20=", forHTTPHeaderField: "Authorization") 
     request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData 

     var paramString = "" 


     for (key, value) in post_data 
     { 
      paramString = paramString + (key as! String) + "=" + (value as! String) + "&" 
     } 

     request.httpBody = paramString.data(using: String.Encoding.utf8) 

     let task = session.dataTask(with: request as URLRequest, completionHandler: { 
      (
      data, response, error) in 

      guard let _:Data = data, let _:URLResponse = response , error == nil else { 

       return 
      } 



      let json: Any? 

      do 
      { 
       json = try JSONSerialization.jsonObject(with: data!, options: []) 
       print(json) 
      } 
      catch 
      { 
       return 
      } 

      guard let server_response = json as? NSDictionary else 
      { 
       return 
      } 


//   if let data_block = server_response["data"] as? NSDictionary 
//   { 
//    if let session_data = data_block["session"] as? String 
//    { 
//     self.login_session = session_data 
//      
//     let preferences = UserDefaults.standard 
//     preferences.set(session_data, forKey: "session") 
//      
//     DispatchQueue.main.async(execute: self.LoginDone) 
//    } 
//   } 
//    


     }) 

     task.resume() 


    } 


    /* 
    // MARK: - Navigation 

    // In a storyboard-based application, you will often want to do a little preparation before navigation 
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    // Get the new view controller using segue.destinationViewController. 
    // Pass the selected object to the new view controller. 
    } 
    */ 

} 
+0

어디에 문제가 있습니까? 보기가 채워지지 않거나 올바르게 구문 분석되지 않습니까? – MacStation

+0

@Prientus 내가 뭘 잘못하고 있는지 알아? –

+0

@MacStation 'MCViewController'가 'UITableViewDataSource'프로토콜을 따르지 않고 빌드되지 않는다고합니다. –

답변

0

UITableViewDataSource를 구현하고 있지만 필수적인 방법은 구현하지 않았습니다.

FUNC에있는 tableView (jQuery과, cellForRowAt : IndexPath) (에서 : jQuery과)

FUNC의 numberOfSections는

이 두 구현하고 프로토콜 오류에 부합하는 것은 멀리 가야하지.

관련 문제