私はコレクションビューをテーブルビューセル内に持っています。モデルは、ウィッシュリスト項目をウィッシュリスト名でグループ化したものです。たくさんの希望リストがあるかもしれません。ユーザがウィッシュリストを選択すると、ウィッシュリストのフラットな配列が得られますが、ウィッシュリストのアイテムイメージのプレビューをセルに表示したいと思います。それらは個別に選択することができず、セル選択は選択されたウィッシュリストの項目のリストに進む。同様に、スワイプの削除動作でウィッシュリスト全体を削除します。UITableViewCellのSwift UICollectionViewが動作しません
ここで私の問題は、collectionView関数が全く起動しないことです。私がテストしているソースデータには、1つのウィッシュリストと4つのアイテムがあります。
ここは私のTableViewCellです。ここで
import Foundation
import UIKit
import Alamofire
import AlamofireImage
class WishListsCell: UITableViewCell {
var collectionView: UICollectionView!
let screenWidth = UIScreen.main.bounds.width
var alamofireRequest: Alamofire.Request?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
layout.itemSize = CGSize(width: screenWidth, height: ((screenWidth/4) * Constants.IMAGE_ASPECT_RATIO))
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
layout.estimatedItemSize.height = ((screenWidth/4) * Constants.IMAGE_ASPECT_RATIO)
layout.estimatedItemSize.width = screenWidth
collectionView = UICollectionView(frame: contentView.frame, collectionViewLayout: layout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(WishListsCollectionViewCell.self, forCellWithReuseIdentifier: cellId)
self.contentView.addSubview(collectionView)
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension WishListsCell: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
debugPrint(wishListItems.count)
return wishListItems.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WishListsCollectionViewCell", for: indexPath) as! WishListsCollectionViewCell
if let imageUrl = wishListItems[indexPath.row]["image"] as? String {
debugPrint(imageUrl)
cell.imageView.image = nil
if let image = ShoppingManager.sharedManager.photoCache.image(withIdentifier: imageUrl) {
cell.imageView.image = image
}
else
{
cell.alamofireRequest?.cancel()
cell.alamofireRequest = Alamofire.request(imageUrl)
.responseImage { response in
let img = response.result.value
cell.imageView.image = img
cell.imageView.contentMode = UIViewContentMode.scaleAspectFit
let imageWidth:CGFloat = self.screenWidth/4
let imageHeight:CGFloat = imageWidth * Constants.IMAGE_ASPECT_RATIO
cell.imageView.frame.size.width = imageWidth
cell.imageView.frame.size.height = imageHeight
ShoppingManager.sharedManager.photoCache.add(img!, withIdentifier: imageUrl)
}
}
}
return cell
}
}
は私CollectionViewCellです:ここでは
import Foundation
import UIKit
import Alamofire
import AlamofireImage
class WishListsCollectionViewCell: UICollectionViewCell {
var alamofireRequest: Alamofire.Request?
var imageView: UIImageView
let screenWidth = UIScreen.main.bounds.width
override init(frame: CGRect) {
imageView = UIImageView()
super.init(frame: frame)
contentView.addSubview(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFit
NSLayoutConstraint.activate([
NSLayoutConstraint(item: imageView, attribute: .width, relatedBy: .equal,
toItem: nil, attribute: .width,
multiplier: 1.0, constant: screenWidth/4),
NSLayoutConstraint(item: imageView, attribute: .height, relatedBy: .equal,
toItem: nil, attribute: .height,
multiplier: 1.0, constant: (screenWidth/4) * Constants.IMAGE_ASPECT_RATIO),
])
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
は私のViewControllerコードの関連部分です:
import UIKit
import Alamofire
import AlamofireImage
import MBProgressHUD
import DBAlertController
class WishListsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView: UITableView = UITableView()
var screenSize: CGRect!
var screenWidth: CGFloat!
var screenHeight: CGFloat!
var cellId = "WishListsCell"
var didSelectItem:Bool = false
override func viewDidLoad() {
super.viewDidLoad()
screenSize = UIScreen.main.bounds
screenWidth = screenSize.width
screenHeight = screenSize.height
tableView.delegate = self
tableView.dataSource = self
tableView.backgroundColor = .white
tableView.register(WishListsCell.self, forCellReuseIdentifier: cellId)
self.view.addSubview(tableView)
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
let customView = UIView(frame: CGRect(x:0, y:0, width:screenWidth, height:30))
customView.backgroundColor = Constants.APP_BACKGROUND_COLOR
let button = UIButton(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 30))
button.setTitle("Add New Wish List", for: .normal)
button.setTitleColor(Constants.APP_TEXT_COLOR, for: .normal)
button.titleLabel?.font = Constants.APP_HEADER_FONT
button.addTarget(self, action: #selector(addButtonAction), for: .touchUpInside)
customView.addSubview(button)
tableView.tableHeaderView = customView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSections(in tableView: UITableView) -> Int {
return wishLists.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 120
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! WishListsCell
let listId = wishLists[indexPath.section]["listid"] as! Int
cell.alamofireRequest?.cancel()
cell.alamofireRequest = Alamofire.request(myURL)
.responseJSON(completionHandler: {
response in
self.parseWishListItemData(JSONData: response.data!)
})
return cell
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let listName = wishLists[section]["listname"] as! String
return listName
}
}
にだけ推測されている必要がありますが、あなたのコレクションビューのデリゲートは、あなたがコレクションビュープロトコルを追加したビューコントローラに設定すべきではありませんか?セットアップのデータソースと代理人はそこにいますか?私はあなたが拡張機能を追加したのを見ています。 –
私はこのようにしたいくつかのブログに従っています。https://contentpedlar.wordpress.com/2016/10/22/uicollectionview-in-uitableview/ – markhorrocks
あなたはメインスレッドでウェブリクエストをしている可能性があります。 UIとそのコレクションビューの更新がブロックされ、なぜ関数呼び出しが得られないのでしょうか。 Alamofireのコードをコメントアウトするとどうなりますか? –