0

我开发了一个菜单界面,其中包含 3 行动态加载的六边形,并允许用户在属于同一类别的不同服务中进行选择。然后我调用了我的 API 端点以获取带有相关数据的 JSON 响应,这也可以正常工作。

问题:第一次打开菜单(即菜单是动态构建的)时,图像显示顺序错误。如果我调用另一个视图然后返回相同的菜单,一切都会正确显示。当然,这种行为很烦人,但到目前为止,我解决这个问题的努力都没有成功。

这是我用于第一行六边形的代码(第二和第三在此之后依次调用:

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)







                        }

                    }

                }
            }
    }
}

任何帮助将不胜感激!非常感谢撒丁岛!

4

0 回答 0