Swift 3 - 如何在自定义 Tabbar 中使用 JSON 数组?
Swift 3 - How to use JSON Array in custom Tabbar?
我在 iOS 更新鲜 Development.I 我正在使用 Tabman 作为 Tab,例如 layout.I 想用我的 json 数组填充标签栏项目。
我的json:-
{
"ManagerName": null,
"ManagerCellNo": null,
"DayDescrList": [
{
"DaySeq": 1,
"Descr": "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda. ",
"DescrSeq": 1,
"HotelId": 1,
"HotelNm": "ABC"
},
{
"DaySeq": 2,
"Descr": "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.",
"DescrSeq": 1,
"HotelId": 1,
"HotelNm": "ABC"
},
{
"DaySeq": 3,
"Descr": "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.",
"DescrSeq": 1,
"HotelId": 2,
"HotelNm": "ABC"
},
{
"DaySeq": 4,
"Descr": "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.",
"DescrSeq": 1,
"HotelId": 3,
"HotelNm": "ABC"
}
]
我想使用“DayDescrList”'DaySeq' 作为我的标签栏项目。
我正在使用 Alamofire 和 SwiftyJSON。
我在代码中尝试的内容:-
func alamoget() {
Alamofire.request("URL").responseJSON(completionHandler: { (responseData) in
if ((responseData.result.value) != nil) {
let swiftyJSONVar = JSON(responseData.result.value!)
if let resData = swiftyJSONVar["DayDescrList"].arrayObject {
self.arrRes = resData as! [[String:AnyObject]]
}
}
})
}
func viewControllers(forPageboyViewController pageboyViewController: PageboyViewController) -> [UIViewController]? {
let storyboard = UIStoryboard(name:"Main",bundle:Bundle.main)
var dict = arrRes[(targetIndex)!]
var viewControllers = [UIViewController] ()
var tabBarItems : [TabmanBar.Item] = []
for i in 0 ..< dict["DaySeq"] {
let vc = storyboard.instantiateViewController(withIdentifier: "childvc") as! childvc
vc.index = i+1
tabBarItems.append(Item(title: String(format: "Day %i",vc.index!)))
viewControllers.append(vc)
}
self.bar.items = tabBarItems
return viewControllers
}
我的viewDidLoad()
方法
override func viewDidLoad() {
super.viewDidLoad()
alamoget()
self.dataSource = self
self.bar.location = .top
}
请帮帮我
仍然没有足够的代码来了解您如何使用数据源,但无论如何
我们需要在收到新数据后立即重新加载标签页:
var arrRes: [[String:AnyObject]] = [] {
didSet{
reloadTabs()
}
}
func reloadTabs(){
//if you have an additionl datasource array, we will prepare a new
guard let controllers = viewControllers(forPageboyViewController: YourPageboyViewController) else{
return
}
//then we need to reload bars in the main queue
DispatchQueue.main.async{ [weak self] in
self?.reloadPages()
}
}
如果方法 viewControllers(forPageboyViewController pageboyViewController: PageboyViewController) 被用作数据源,只需在 alamofire 完成中添加 reloadPages() 并且不需要添加任何其他代码:
func alamoget() {
Alamofire.request("URL").responseJSON(completionHandler: { (responseData) in
if ((responseData.result.value) != nil) {
let swiftyJSONVar = JSON(responseData.result.value!)
if let resData = swiftyJSONVar["DayDescrList"].arrayObject {
self.arrRes = resData as! [[String:AnyObject]]
DispatchQueue.main.async{ [weak self] in
self.reloadPages()
}
}
}
})
}
我在 iOS 更新鲜 Development.I 我正在使用 Tabman 作为 Tab,例如 layout.I 想用我的 json 数组填充标签栏项目。
我的json:-
{
"ManagerName": null,
"ManagerCellNo": null,
"DayDescrList": [
{
"DaySeq": 1,
"Descr": "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda. ",
"DescrSeq": 1,
"HotelId": 1,
"HotelNm": "ABC"
},
{
"DaySeq": 2,
"Descr": "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.",
"DescrSeq": 1,
"HotelId": 1,
"HotelNm": "ABC"
},
{
"DaySeq": 3,
"Descr": "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.",
"DescrSeq": 1,
"HotelId": 2,
"HotelNm": "ABC"
},
{
"DaySeq": 4,
"Descr": "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.",
"DescrSeq": 1,
"HotelId": 3,
"HotelNm": "ABC"
}
]
我想使用“DayDescrList”'DaySeq' 作为我的标签栏项目。 我正在使用 Alamofire 和 SwiftyJSON。
我在代码中尝试的内容:-
func alamoget() {
Alamofire.request("URL").responseJSON(completionHandler: { (responseData) in
if ((responseData.result.value) != nil) {
let swiftyJSONVar = JSON(responseData.result.value!)
if let resData = swiftyJSONVar["DayDescrList"].arrayObject {
self.arrRes = resData as! [[String:AnyObject]]
}
}
})
}
func viewControllers(forPageboyViewController pageboyViewController: PageboyViewController) -> [UIViewController]? {
let storyboard = UIStoryboard(name:"Main",bundle:Bundle.main)
var dict = arrRes[(targetIndex)!]
var viewControllers = [UIViewController] ()
var tabBarItems : [TabmanBar.Item] = []
for i in 0 ..< dict["DaySeq"] {
let vc = storyboard.instantiateViewController(withIdentifier: "childvc") as! childvc
vc.index = i+1
tabBarItems.append(Item(title: String(format: "Day %i",vc.index!)))
viewControllers.append(vc)
}
self.bar.items = tabBarItems
return viewControllers
}
我的viewDidLoad()
方法
override func viewDidLoad() {
super.viewDidLoad()
alamoget()
self.dataSource = self
self.bar.location = .top
}
请帮帮我
仍然没有足够的代码来了解您如何使用数据源,但无论如何
我们需要在收到新数据后立即重新加载标签页:
var arrRes: [[String:AnyObject]] = [] {
didSet{
reloadTabs()
}
}
func reloadTabs(){
//if you have an additionl datasource array, we will prepare a new
guard let controllers = viewControllers(forPageboyViewController: YourPageboyViewController) else{
return
}
//then we need to reload bars in the main queue
DispatchQueue.main.async{ [weak self] in
self?.reloadPages()
}
}
如果方法 viewControllers(forPageboyViewController pageboyViewController: PageboyViewController) 被用作数据源,只需在 alamofire 完成中添加 reloadPages() 并且不需要添加任何其他代码:
func alamoget() {
Alamofire.request("URL").responseJSON(completionHandler: { (responseData) in
if ((responseData.result.value) != nil) {
let swiftyJSONVar = JSON(responseData.result.value!)
if let resData = swiftyJSONVar["DayDescrList"].arrayObject {
self.arrRes = resData as! [[String:AnyObject]]
DispatchQueue.main.async{ [weak self] in
self.reloadPages()
}
}
}
})
}