0

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

在此处输入图像描述

我通过以下代码在 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 文件的子视图如下:

在此处输入图像描述

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

在此处输入图像描述

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

在此处输入图像描述

我从 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

}
4

1 回答 1

0

抱歉,原型单元仅在基于故事板的项目中可用。希望您不会对 xib 方法走得太远,而是可以尝试多个故事板。如果你觉得它们有点压倒性,没关系;他们变得更好了——请注意,您不必为所有视图控制器都拥有一个故事板。您可以拥有多个,或者在代码中链接或使用故事板引用。

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

于 2018-02-25T10:15:25.843 回答