我有一个带有 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
}