这里我尝试实现集合视图但无法实现它并且在滚动视图上我放置了集合视图是否正确实现如果不是这样你可以提供任何解决方案来显示在ui和 在这个滚动视图中实现了视图,所有元素都放在它上面这里是我的代码以及如何缩放选定的特定图像也可以有人帮助我吗?
@IBOutlet var collectionView: UICollectionView!
@IBOutlet var navigationBar: UINavigationBar!
@IBOutlet var imageView: UIImageView!
@IBOutlet var pageControl: UIPageControl!
@IBOutlet var nameLabel: UILabel!
@IBOutlet var priceLabel: UILabel!
@IBOutlet var textview: UITextView!
@IBOutlet var scrollView: UIScrollView!
var productName = [String]()
var productprice = [String]()
var productdescription :String?
var thumbnailimageArray = [String]()
var imageArray = [String]()
var pageIndex: Int = 0
var imagesArray = [String]()
let urlString = "http://www.json-generator.com/api/json/get/cjpberBhKa?indent=2"
override func viewDidLoad() {
super.viewDidLoad()
self.downloadJsonWithURL()
collectionView.delegate = self
collectionView.dataSource = self
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swiped(gesture:)))
swipeLeft.direction = .left
self.imageView.addGestureRecognizer(swipeLeft)
swipeLeft.cancelsTouchesInView = false
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swiped(gesture:)))
swipeRight.direction = .right
self.imageView.addGestureRecognizer(swipeRight)
swipeRight.cancelsTouchesInView = false
imageView.isUserInteractionEnabled = true
// 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.append(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.append(price as! String)
}
if let description = detailDict.value(forKey: "productDes") {
self.productdescription = description as? String
}
if let image = detailDict.value(forKey: "img"){
self.imagesArray.append(image as! String)
}
}
}
OperationQueue.main.addOperation({
self.navigationBar.topItem?.title = self.productName[0]
self.textview.text = self.productdescription
self.priceLabel.text = self.productprice[0]
self.nameLabel.text = self.productName[0]
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
}
let imgURL = NSURL(string:self.imageArray[pageIndex])
let data = NSData(contentsOf: (imgURL as URL?)!)
self.imageView.image = UIImage(data: data! as Data)
pageControl.numberOfPages = imageArray.count
pageControl.currentPage = pageIndex
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)
pageControl.numberOfPages = imageArray.count
pageControl.currentPage = pageIndex
default:
break
}
}
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imagesArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell",for:indexPath) as! DetailsCell
let arr = imagesArray[indexPath.row]
let urls = NSURL(string: arr)
let data = NSData (contentsOf: urls! as URL)
cell.imageView.image = UIImage(data: data! as Data)
cell.nameLabel.text = self.productName[indexPath.row]
cell.priceLabel.text = self.productprice[indexPath.row]
cell.layer.borderColor = UIColor.lightGray.cgColor
cell.layer.borderWidth = 0.7
return cell
}
答案 0 :(得分:0)
在下载了JsonWithURL后,您需要重新加载collectionview的数据。
答案 1 :(得分:0)
从collection view
获取所有data
之后重新加载web service response
。
collectionView.reloadData()