我更喜欢iOS开发。我正在使用Tabman for tab like layout.I想用我的json数组填充tabbar项目。
我的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'作为我的tabbar项目。 我正在使用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
}
请帮帮我
答案 0 :(得分:1)
仍然没有足够的代码来获取您使用数据源的方式,但无论如何
我们需要在收到新数据后立即重新加载标签:
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()
}
}
}
})
}