I have developed a menu interface that has 3 rows of hexagons that load dynamically and allow the user to choose among different services belonging to the same category. I have then called my API end-point to obtain the JSON response with the relevant data and this is also working fine.
THE ISSUE: The first time the menu is opened (i.e the menu is dynamically built) the images are shown in wrong order. If I call another view and then go back to the same menu, everything is shown correctly. Of course, this behaviour is quite annoying, but up to now, my efforts to resolve this have been unsuccessfull.
Here the piece of code I use for the first row of hexagons (second and third are called in sequence after this similarly:
func readJSONforPacketsRow1(language:Int, category:Int, location:String){
print ("FIRST ROW")
Alamofire.request(.GET, "\(URL.base)packets/1,\(language),\(category),\(location)", parameters:nil, encoding: ParameterEncoding.URL)
.responseJSON { response in
let json = JSON(data: response.data!)
if let JSON = response.result.value {
for packet in json.arrayValue
{
let name = packet["name"].stringValue
let name_second = packet["name_second"].stringValue
let image = packet["image"].stringValue
let description = packet["description"].stringValue
let language = packet["language"].stringValue
let id = packet["id"].intValue
let packet_type = packet["packet_type"].intValue
let mypacket = Packet(id: id, packet_type: packet_type, name: name, name_second: name_second, description: description, image: image, language: language)
self.packetArray.append(mypacket)
}
if (self.packetArray.count > 0){
//First row
self.packetArray.sortInPlace{$0.id < $1.id}
var buttonX: CGFloat = 60 // our Starting Offset, could be 0
for i in 0..<self.packetArray.count {
self.indicator.startAnimating()
let URLc = NSURL(string: "\(URL.base)packetimages/\(self.packetArray[i].image)")!
let fetcher = NetworkFetcher<UIImage>(URL: URLc)
let cachedEntryKey = fetcher.key
self.cache.fetch(key:cachedEntryKey).onSuccess { image in
let imageData = UIImagePNGRepresentation(image)!
let cachedImage = UIImage(data: imageData, scale: 1)!
let button = UIButton(frame: CGRect(x: buttonX, y: 30, width: self.hexwidth, height: self.hexheight))
buttonX = buttonX + self.hexmargin
button.tintColor = UIColor.whiteColor()
button.backgroundColor = UIColor.clearColor()
button.setTitleColor(UIColor.blackColor(), forState: .Normal)
button.setImage(SliderImageButton.imageOfSliderButton(cachedImage, isSelected: false, text: "\(self.packetArray[i].name)", text2: "\(self.packetArray[i].name_second)"), forState: .Normal)
button.setImage(SliderImageButton.imageOfSliderButton(cachedImage, isSelected: true, text: "\(self.packetArray[i].name)", text2: "\(self.packetArray[i].name_second)"), forState: .Highlighted)
button.addTarget(self, action: #selector(PacketMenu.clickMe(_:)), forControlEvents: UIControlEvents.TouchUpInside)
button.tag = self.packetArray[i].id
self.view.addSubview(button)
}
}
}
}
}
}
Any help will be greatly appreciated! Many thanks from Sardinia!