【发布时间】:2022-01-16 05:32:42
【问题描述】:
我有一个下拉列表,我想用来自 Alamofire 发布请求的数据填充它。
我之前使用 SwiftyJSON 来填充表格视图。现在我添加了 Dropbox-iOS-SDK,希望我能找到如何填充下拉列表。
AF.request(URL_COURSES, method: .post).responseJSON
{
response in
//printing response
//print(response)
switch response.result {
case .success (let value):
let json = JSON(value)
for (key,subJson):(String, JSON) in json["courses"] {
debugPrint (key) //key
//debugPrint (subJson) //value
self.myCourses.append(MyCourses(courseId: subJson["courseid"].rawValue as! Int,
courseName: subJson["coursename"].rawValue as! String))
}
case .failure(let error):
print(error)
}
self.dropDown.anchorView = self.viewDropDown
self.dropDown.dataSource = //here i would like to see the list of courseId and courseName or only courseName
dropDown.selectionAction = { [unowned self] (index: Int, item: String) in
print("Selected item: \(item) at index: \(index)")
self.labelTitle.text = coursesArray[index]
}
我用一个数组做了一个测试,下拉菜单工作正常。
{
@IBOutlet weak var viewDropDown: UIView!
@IBOutlet weak var labelTitle: UILabel!
let dropDown = DropDown()
let coursesArray = ["Mathematic 1","Geograhic 1","Astrophysics 1","English 1","French 1"]
override func viewDidLoad() {
super.viewDidLoad()
labelTitle.text = "Select a Course"
viewDropDown.layer.cornerRadius = 5
dropDown.anchorView = viewDropDown
dropDown.dataSource = coursesArray
dropDown.bottomOffset = CGPoint(x: 0, y:(dropDown.anchorView?.plainView.bounds.height)!)
dropDown.direction = .bottom
DropDown.appearance().setupCornerRadius(5)
DropDown.appearance().textColor = UIColor.lightGray
dropDown.selectionAction = { [unowned self] (index: Int, item: String) in
print("Selected item: \(item) at index: \(index)")
self.labelTitle.text = coursesArray[index]
}
}
@IBAction func showCoursesOptions(_ sender: Any) {
dropDown.show()
}
}
【问题讨论】:
-
不相关但 AF 5 可以直接解码为带有
Decodable的模型。SwiftyJSON是一个很棒的工具,但同时已经过时了。实际上你做了三个转换 JSON -> Dictionary > SwiftyJSON -> Model。这是非常低效的 -
"我有一个下拉列表" 什么是下拉列表?一个叫“DropDown”的人是从哪里来的?
-
在我的示例中,您将如何使用带有 Decodable 的模型?
标签: ios swift drop-down-menu alamofire