我一直在故障排除显示UIImageView,我不知道它哪里不让我。
MainViewController
class MainViewController: UIViewController {
@IBOutlet weak var mainTableView: UITableView!
var imageArray : [PhotoModel] = []
var images = [UIImage]()
var imageRatio = [CGFloat]()
var currentIndex = 0
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.hidesBackButton = true
let dispatchGroup = DispatchGroup()
dispatchGroup.enter()
PhotoManager().fetchPhotos(query: "") { (data, error) in
self.imageArray = data
for d in data {
let url = URL(string: d.url)
let imageURL = try? Data(contentsOf: url!)
let image = UIImage(data: imageURL!)
self.images.append(image!)
}
for d in data {
let ratio = CGFloat(d.width / d.height)
self.imageRatio.append(ratio)
}
dispatchGroup.leave()
}
dispatchGroup.notify(queue: .main){
print("reloadingData")
self.mainTableView.reloadData()
}
}
}
extension MainViewController: UITableViewDataSource, UITableViewDelegate {
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return imageArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "imageCell", for: indexPath) as! ImageTableViewCell
DispatchQueue.main.async {
cell.randomImageView?.image = self.images[indexPath.row]
cell.nameLabel?.text = self.imageArray[indexPath.row].username
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return tableView.frame.width/imageRatio[indexPath.row]
}
//tableView delegate
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
currentIndex = indexPath.row
print(imageArray[currentIndex].name)
performSegue(withIdentifier: "full", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destination = segue.destination as! FullViewController
destination.images = self.images
destination.currentIndex = self.currentIndex
}
}
FullViewController
class FullViewController: UIViewController {
@IBOutlet weak var fullCollectionView: UICollectionView!
var images = [UIImage]()
var currentIndex = 0
override func viewDidLoad() {
super.viewDidLoad()
fullCollectionView.isPagingEnabled = true
}
override func viewWillAppear(_ animated: Bool) {
fullCollectionView.selectItem(at: IndexPath(item: currentIndex, section: 0), animated: true, scrollPosition: .centeredHorizontally)
}
}
extension FullViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! FullImageCollectionViewCell
cell.fullScreenImages.image = images[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
}
ImageTableViewCell
class ImageTableViewCell: UITableViewCell {
@IBOutlet weak var randomImageView: UIImageView?
@IBOutlet weak var nameLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
UIImage:0x600001e641b0匿名{1080 1080}>
所有的间距标签似乎都起作用了。
有没有显示UIImageview的选项?
谢谢!