分段控件中的自定义UIView内的UITableView不会在自定义单元格中显示JSON值

时间:2018-02-23 11:55:32

标签: swift uitableview uiview custom-cell segmentedcontrol

我有一个带有UIView的分段控件,如下所示:

enter image description here

我在UIView中添加了两个自定义视图,代码如下:

class NotificationViewController: UIViewController {

@IBOutlet weak var viewContainer: UIView!

//create a variable for view
var views : [UIView]!

override func viewDidLoad() {
    super.viewDidLoad()

    //initialize the view
    views = [UIView]()

    //appened the view inside the views array
    views.append(ImportantNotification().view)
    views.append(GeneralNotificaton().view)

    //start the loop to add the subviews inside the view
    for v in views{
        viewContainer.addSubview(v)
    }

    //bring the default view to the front while we launch it
    viewContainer.bringSubview(toFront: views[0])
}

@IBAction func notificationSegemntsPressed(_ sender: UISegmentedControl) {
    //finally bring the subview inside the segmented view
    self.viewContainer.bringSubview(toFront: views[sender.selectedSegmentIndex])
   }
 }

它有效!

带有.xib文件的子视图如下:

enter image description here

我在第一个子视图中保留了一个UI表视图,如下所示:

enter image description here

而且,我为第一个子视图制作了一个自定义单元格,如下所示:

enter image description here

我从api加载了Json数据,并希望在自定义单元格的表格视图中显示它,并且JSON数据已成功加载,但它不会填充在表格视图中。 我在表视图中加载数据的代码如下所示:

import UIKit

class ImportantNotification: UIViewController, UITableViewDelegate,        UITableViewDataSource {

@IBOutlet weak var importantNotificationTableView: UITableView!

var nontificatonData =  [NotificationDataModel]()

override func viewDidLoad() {
    super.viewDidLoad()

    importantNotificationTableView.delegate = self 
    importantNotificationTableView.dataSource = self

    let nib  = UINib(nibName: "TableViewCell", bundle: nil)
    importantNotificationTableView.register(nib, forCellReuseIdentifier: "customCell")

    downloadJSON {

        self.importantNotificationTableView.reloadData()
    }



}

func downloadJSON(completed: @escaping () -> () )  {
    guard let url =   URL(string : "http://www.something.com/notice/get") else {return}
    var request = URLRequest.init(url: url)
    request.httpMethod = "POST"

    request.addValue("cf7ab8c9d4efae82b575eabd6bec76cbb8c6108391e036387f3dd5356a582171519367747000", forHTTPHeaderField: "app_key")

    let postDictonary = "school_id=1"

    //send value directly to server without chaging to json
    request.httpBody = postDictonary.data(using: .utf8)



    URLSession.shared.dataTask(with: request) { (data, response, error) in
        if error == nil{
            do{
                self.nontificatonData = try JSONDecoder().decode([NotificationDataModel].self, from: data!)
                print(self.nontificatonData)
                DispatchQueue.main.async {

                    completed()
                }
            }catch{
                print(error)
            }
        }
        }.resume()
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return nontificatonData.count
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! TableViewCell
    cell.lblTitileNotification.text = nontificatonData[indexPath.row].notice_title
    cell.lblDiscriptionNotificaiton.text = nontificatonData[indexPath.row].notice_desc



    return cell
}


}

我的结构如下:

import Foundation

struct NotificationDataModel : Decodable{
let notice_id : String
let notice_title : String
let notice_desc : String
let notice_date : String
let content_name : String
let notice_link : String
let is_important : String
let parent_availability : String
let is_pinned : String
let created_at : String

}

1 个答案:

答案 0 :(得分:0)

对不起,原型单元格仅在基于故事板的项目中可用。希望你不会在xib方法上走得太远而可以尝试多个故事板。如果你发现它们有点压倒性,那就没问题;它们会变得更好 - 请注意,您不必为所有视图控制器配备一个故事板。您可以使用多个代码链接或使用故事板引用。

如果你想坚持使用xibs,另一种选择是在代码中使用registerNib。因此,您在xibs中设计原型单元,然后在代码中注册它们。不太顺利,但它可能适合您的需求。