ここで、json Web サービスからデータを取得できても、プログラムの ui に表示できないという問題が発生しています。エラーが発生している場所と、そこに記載されていることも、誰か助けてもらえますか?
class DetailsViewController: UIPageViewController{
@IBOutlet var navigationBar: UINavigationBar!
@IBOutlet var imageView: UIImageView!
@IBOutlet var pageControl: UIPageControl!
@IBOutlet var nameLabel: UILabel!
@IBOutlet var priceLabel: UILabel!
@IBOutlet var textview: UITextView!
var productName :String?
var productprice :String?
var productdescription :String?
var thumbnailimageArray = [String]()
var imageArray = [String]()
var pageIndex: Int = 0
let urlString = "http://www.json-generator.com/api/json/get/cjpberBhKa?indent=2"
override func viewDidLoad() {
super.viewDidLoad()
self.downloadJsonWithURL()
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
swipeLeft.direction = .left
self.imageView.addGestureRecognizer(swipeLeft) //unexpectely found nil while unwrapping an optional value
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
swipeRight.direction = .right
self.imageView.addGestureRecognizer(swipeRight)
imageView.isUserInteractionEnabled = true
pageControl.numberOfPages = imageArray.count
pageControl.currentPage = pageIndex
// Do any additional setup after loading the view.
}
func downloadJsonWithURL() {
let url = NSURL(string: urlString)
URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in
if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {
print(jsonObj!.value(forKey: "Detail")!)
if let detailsArray = jsonObj!.value(forKey: "Detail") as? NSArray {
if let detailDict = detailsArray[0] as? NSDictionary {
if let name = detailDict.value(forKey: "productName") {
self.productName = name as? String
}
if let image1 = detailDict.value(forKey: "image1"){
self.imageArray.append(image1 as! String)
}
if let image2 = detailDict.value(forKey: "image2"){
self.imageArray.append(image2 as! String)
}
if let image3 = detailDict.value(forKey: "image3"){
self.imageArray.append(image3 as! String)
}
if let image4 = detailDict.value(forKey: "image4"){
self.imageArray.append(image4 as! String)
}
if let image5 = detailDict.value(forKey: "image5"){
self.imageArray.append(image5 as! String)
}
if let image6 = detailDict.value(forKey: "image6"){
self.imageArray.append(image6 as! String)
}
if let image7 = detailDict.value(forKey: "image7"){
self.imageArray.append(image7 as! String)
}
if let image8 = detailDict.value(forKey: "image8"){
self.imageArray.append(image8 as! String)
}
if let image9 = detailDict.value(forKey: "image9"){
self.imageArray.append(image9 as! String)
}
if let image10 = detailDict.value(forKey: "image10"){
self.imageArray.append(image10 as! String)
}
if let price = detailDict.value(forKey: "productPrice") {
self.productprice = price as? String
}
if let description = detailDict.value(forKey: "productDes") {
self.productdescription = description as? String
}
}
}
OperationQueue.main.addOperation({
self.navigationBar.topItem?.title = self.productName
self.textview.text = self.productdescription
self.priceLabel.text = self.productprice
print(self.imageArray)
let imgURL = NSURL(string:self.imageArray[0])
let data = NSData(contentsOf: (imgURL as URL?)!)
self.imageView.image = UIImage(data: data! as Data)
})
}
}).resume()
}
func swiped(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right :
if pageIndex == 0 {
}else{
pageIndex -= 1
}
imageView.image = UIImage(named: imageArray[pageIndex])
case UISwipeGestureRecognizerDirection.left:
if pageIndex >= imageArray.count-1{
}else{
pageIndex += 1
}
imageView.image = UIImage(named: imageArray[pageIndex])
default:
break
}
}
}
func swiped(gesture: UIGestureRecognizer)
メソッド実行で画像を表示するために使用しているのは次のとおりです
imageView.image = UIImage(named: imageArray[pageIndex])
しかし、あなたの画像はサーバーからコンピングされているので、あなたの製品のすべての画像を表示する必要があるだけです.最初に表示したのと同じOperationQueue.main.addOperation
ように画像を表示する代わりに、UIImage(named: imageArray[pageIndex])
それをデータに変換してimageviewに表示する必要があり、あなたの変更func swiped(gesture: UIGestureRecognizer)
は次のように見えるかもしれません以下のように
func swiped(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right :
if pageIndex == 0 {
}else{
pageIndex -= 1
}
let imgURL = NSURL(string:self.imageArray[pageIndex])
let data = NSData(contentsOf: (imgURL as URL?)!)
self.imageView.image = UIImage(data: data! as Data)
case UISwipeGestureRecognizerDirection.left:
if pageIndex >= imageArray.count-1{
}else{
pageIndex += 1
}
let imgURL = NSURL(string:self.imageArray[pageIndex])
let data = NSData(contentsOf: (imgURL as URL?)!)
self.imageView.image = UIImage(data: data! as Data)
default:
break
}
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加