我有一个简单的应用程序,其表格视图控制器包含几个部分的文章,例如体育部分3篇文章:
当我打开本节中的特定文章时,我希望能够向左滑动以转到下一篇文章,
我该怎么办?
答案 0 :(得分:1)
基本上,您需要创建
articles
。
根据{{1}},您可以填充tableView
。articles
设置此viewcontroller。答案 1 :(得分:0)
这对我有用。
当检测到向左滑动时,会调用handleTap。 productNumberInPage递增。这是从NetworkManager单例中查找数据的索引。这不是必需的,只是我的实现。
在productNumberInPage递增后,调用setupViews。这导致页面被新数据刷新。你也可以在这里添加一个动画。
要实现,请在详细视图控制器中添加手势识别器,以在详细视图控制器中调用setupViews函数。
class ProductDetailVC: UIViewController {
// MARK: - Variables
var productNumberInPage = 0
// MARK: - IBOutlets
@IBOutlet weak var productNameLabel: UILabel!
@IBOutlet weak var reviewRatingLabel: CosmosView!
@IBOutlet weak var reviewCountLabel: UILabel!
@IBOutlet weak var productImage: UIImageView!
@IBOutlet weak var priceLabel: UILabel!
@IBOutlet weak var freeShippingLabel: UILabel!
@IBOutlet weak var inStockLabel: UILabel!
@IBOutlet weak var shortDescription: UITextView!
@IBOutlet weak var longDescription: UITextView!
@IBOutlet weak var contentView: UIView!
// MARK: - Functions
override func viewDidLoad() {
super.viewDidLoad()
let recognizer: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(recognizer:)))
recognizer.direction = .left
self.view.addGestureRecognizer(recognizer)
setUpView()
}
func setUpView() {
guard let products = NetworkManager.instance.allProducts[pageWithTV].products else { return }
productNameLabel.text = products[productNumberInPage].productName ?? "N/A"
self.reviewRatingLabel.rating = products[productNumberInPage].reviewRating ?? 0
reviewCountLabel.text = "\(products[productNumberInPage].reviewCount ?? 0)"
priceLabel.text = products[productNumberInPage].price ?? "N/A"
shortDescription.text = (products[productNumberInPage].shortDescription ?? "Not available.").html2String
longDescription.text = (products[productNumberInPage].longDescription ?? "Not available.").html2String
if let inStock = products[productNumberInPage].inStock, inStock {
inStockLabel.text = IN_STOCK
inStockLabel.textColor = ColorPalette.Green.Medium
} else {
inStockLabel.text = OUT_OF_STOCK
inStockLabel.textColor = ColorPalette.Red.Medium
freeShippingLabel.textColor = ColorPalette.White.Medium
}
image from a URL
let image = UIImage(named: LOADING_IMAGE)
guard let url = products[productNumberInPage].productImage else { return }
DispatchQueue.main.async {
self.productImage.kf.setImage(with: url, placeholder: image)
}
}
@objc func handleSwipe(recognizer : UISwipeGestureRecognizer) {
guard productNumberInPage != (PAGE_SIZE - 1) else {
print("Sorry out of products on this page. Go back to list view")
return
}
productNumberInPage += 1
setUpView()
}
}