Ios 我曾尝试在ui上显示集合视图,但无法显示。有人能帮我吗?
在这里,我试图实现集合视图,但无法实现,在滚动视图中,我放置了集合视图。这样实现是否正确?如果不正确,您能否提供任何解决方案,使其显示在ui和 在这个滚动视图上实现了视图,所有的元素都放在它上面。这是我的代码,如何缩放选定的特定图像,还有人能帮我吗Ios 我曾尝试在ui上显示集合视图,但无法显示。有人能帮我吗?,ios,swift3,uicollectionview,Ios,Swift3,Uicollectionview,在这里,我试图实现集合视图,但无法实现,在滚动视图中,我放置了集合视图。这样实现是否正确?如果不正确,您能否提供任何解决方案,使其显示在ui和 在这个滚动视图上实现了视图,所有的元素都放在它上面。这是我的代码,如何缩放选定的特定图像,还有人能帮我吗 @IBOutlet var collectionView: UICollectionView! @IBOutlet var navigationBar: UINavigationBar! @IBOutlet var image
@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
}
下载JSONWITHURL loaded后,您需要重新加载collectionview的数据。从
web服务响应中获取所有数据后,重新加载集合视图
,如下所示
collectionView.reloadData()
你不久前用同样的代码发布了一个问题,甚至接受了一个答案。你在寻找答案吗???@Vamsi你现在展示的是什么?您可以提供图像。@bilal我试图在同一任务中实现图像视图,但无法在其上显示,这就是我发布EDI phone 7显示大小的原因我试图显示图像,但无法发布这是图像链接@Aircraft如何重新加载集合视图以及用于此的代码是什么?func downloadJsonWithURL(){let url=NSURL(string:urlString)URLSession.shared.dataTask(with:(url作为url?),completionHandler:{(数据,响应,错误)->如果let jsonObj=try?JSONSerialization.jsonObject(with:data!,options:.allowFragments)作为?NSDictionary{//您的代码}则无效DispatchQueue.main.async{//在此处更新您的UI//在此处重新加载collectionView数据collectionView.reloadData()})。resume()}
它在ui上只有集合视图时工作,但我的页面是这样的,它不工作I.stack.imgur.com/QWYQr.pngI没有理解你说的。它在ui上只有集合视图时工作
。那么你的集合视图在哪里I.stack.imgur.com/QWYQr.png你只需检查一下图片,我正在展示c描述下面的收集视图