问题描述
在我的收藏视图中,我在最后一个单元格的末尾获得了额外的空间。我怎样才能停止这个额外的空间?
This 是我得到的结果。
这是我的 ViewController.swift 文件代码
class ViewController: UIViewController {
@IBOutlet weak var tblViewDeal: UITableView!
let arrCarousel = [AssestimageName.deal1,AssestimageName.deal2,AssestimageName.deal3,AssestimageName.deal4,AssestimageName.deal5]
var carouselTVCell = CarouselTableViewCell()
var counter = 0
var isSelectedindex = 0
var shouldLoadData = false
var selectedHeaderCell = 10
var dealModelResponse: DealModel?
var currentPage = 1
var totalCount = 0
override func viewDidLoad() {
super.viewDidLoad()
getData(pageNo: currentPage,categoryId: 2)
}
fileprivate func getData(pageNo: Int,categoryId: Int) {
APIFunction.sharedInstance.getDealData(pageNo: pageNo,categoryId: categoryId) { [self] (responseData) in
let jsonDecoder = JSONDecoder()
do {
let resObj = try jsonDecoder.decode(DealModel.self,from: responseData)
print("resObj")
currentPage = pageNo
totalCount = resObj.totalCount ?? 0
print("currentPage = \(currentPage)")
if pageNo == 1 {
dealModelResponse = resObj
} else {
if resObj.productList?.count ?? 0 > 0 {
for product in resObj.productList! {
dealModelResponse?.productList?.append(product)
}
}
}
dispatchQueue.main.async { [self] in
shouldLoadData = false
tblViewDeal.reloadData()
}
} catch let error {
print("error occurred while decoding = \(error.localizedDescription)")
}
} failure: { (error) in
print(error)
}
}
}
//MARK: - UITableViewDelegate,UITableViewDataSource
extension ViewController: UITableViewDelegate,UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tblViewDeal.dequeueReusableCell(withIdentifier: "cellCarousel",for: indexPath) as! CarouselTableViewCell
if indexPath.row == 0 {
cell.pageView.numberOfPages = arrCarousel.count
cell.pageView.currentPage = 0
carouselTVCell = cell
}
cell.carouselCV.tag = 100
cell.carouselCV.reloadData()
return cell
} else {
let cell = tblViewDeal.dequeueReusableCell(withIdentifier: "ProductTableViewCell",for: indexPath) as! ProductTableViewCell
cell.productCVCell.register(UINib(nibName: "ProductCVCell",bundle: nil),forCellWithReuseIdentifier: "ProductCVCell")
cell.productCVCell.tag = 300
cell.productCVCell.delegate = self
cell.productCVCell.dataSource = self
cell.productCVCell.reloadData()
cell.productCVCell.layoutIfNeeded()
return cell
}
}
func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView,viewForHeaderInSection section: Int) -> UIView? {
if section == 1 {
let headerView = tableView.dequeueReusableheaderfooterView(withIdentifier: "DealHeaderView") as! DealHeaderView
headerView.cvDealHeader.register(UINib(nibName: "HeaderCVCell",forCellWithReuseIdentifier: "HeaderCVCell")
headerView.cvDealHeader.tag = 200
headerView.cvDealHeader.dataSource = self
headerView.cvDealHeader.delegate = self
headerView.cvDealHeader.reloadData()
return headerView
} else {
return UIView()
}
}
func tableView(_ tableView: UITableView,heightForHeaderInSection section: Int) -> CGFloat {
if section == 1 {
return 40.0
} else {
return 0.0
}
}
fileprivate func loadMoreData() {
let footerView = UIView(frame: CGRect(x: 0,y: 0,width: self.view.frame.size.width,height: 30.0))
footerView.backgroundColor = UIColor.clear
let lblLoading = UILabel(frame: CGRect(x: 0,width: footerView.frame.width,height: footerView.frame.height))
lblLoading.text = "Loading..."
lblLoading.font = UIFont(name: "Font_MuSEOSans",size: 14.0)
lblLoading.textAlignment = .center
lblLoading.autoresizingMask = .flexibleWidth
lblLoading.textColor = UIColor.darkGray
footerView.addSubview(lblLoading)
tblViewDeal.tableFooterView = footerView
getData(pageNo: currentPage + 1,categoryId: 2)
}
}
//MARK: - UICollectionViewDelegate,UICollectionViewDataSource
extension ViewController: UICollectionViewDelegate,UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
switch collectionView.tag {
case 100: return arrCarousel.count
case 200: return dealModelResponse?.categories?.count ?? 0//arrHeader.count
case 300: return dealModelResponse?.productList?.count ?? 0//2
default:
return 0
}
}
func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
switch collectionView.tag {
case 100:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "carouselCV",for: indexPath) as! CarouselCollectionViewCell
cell.imageView.image = UIImage(named: arrCarousel[indexPath.row])
return cell
case 200:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HeaderCVCell",for: indexPath) as! HeaderCVCell
cell.lblHeader.text = dealModelResponse?.categories?[indexPath.item].name
if isSelectedindex == indexPath.row {
cell.headerBgView.backgroundColor = UIColor.black
cell.lblHeader.textColor = UIColor.white
} else {
cell.headerBgView.backgroundColor = UIColor.white
cell.lblHeader.textColor = UIColor.black
}
return cell
case 300:
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ProductCVCell",for: indexPath) as? ProductCVCell {
cell.imgViewProduct.imageFromServerURL(dealModelResponse?.productList?[indexPath.item].thumbNail ?? "",placeHolder: UIImage(named: "deal4"))
cell.lblProductName.text = dealModelResponse?.productList?[indexPath.item].name
cell.lblProductOriginCountry.text = "USA"
cell.lblProductQty.text = "230 gm"
cell.lblProductPrice.text = dealModelResponse?.productList?[indexPath.item].formattedFinalPrice
return cell
} else {
return UICollectionViewCell()
}
default:
return UICollectionViewCell()
}
}
func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath) {
if collectionView.tag == 200 {
isSelectedindex = indexPath.row
selectedHeaderCell = 10 + indexPath.row
tblViewDeal.reloadData()
tblViewDeal.scrollToRow(at: IndexPath(row: 0,section: 0),at: .top,animated: true)
}
}
}
//MARK: - UICollectionViewDelegateFlowLayout
extension ViewController: UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,insetForSectionAt section: Int) -> UIEdgeInsets {
switch collectionView.tag {
case 300: return UIEdgeInsets(top: 10,left: 10,bottom: 0,right: 10)
default:
return UIEdgeInsets(top: 0,left: 0,right: 0)
}
}
func collectionView(_ collectionView: UICollectionView,sizeforItemAt indexPath: IndexPath) -> CGSize {
switch collectionView.tag {
case 100:
let size = collectionView.frame.size
return CGSize(width: size.width,height: size.height)
case 200:
return CGSize(width: 130.0,height: 38.0)
case 300:
return CGSize(width: (collectionView.bounds.width - 30)/2,height: 250.0)
default:
return CGSize(width: 0.0,height: 0.0)
}
}
func collectionView(_ collectionView: UICollectionView,minimumLinespacingForSectionAt section: Int) -> CGFloat {
switch collectionView.tag {
case 200: return 1.0
case 300: return 10.0
default:
return 0.0
}
}
func collectionView(_ collectionView: UICollectionView,minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
switch collectionView.tag {
case 300: return 10.0
default:
return 0.0
}
}
}
//MARK: - ScrollView Methods
extension ViewController: uiscrollviewdelegate {
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if !shouldLoadData {
print("contentOffset = \(scrollView.contentOffset.y)")
print("difference = \(scrollView.contentSize.height - scrollView.frame.size.height)")
let contentOffset = scrollView.contentOffset.y
let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height
if maximumOffset - contentOffset <= 10 {
// if(scrollView.contentOffset.y == (scrollView.contentSize.height - scrollView.frame.size.height)) {
if totalCount > self.dealModelResponse?.productList?.count ?? 0 {
print("scrollViewDidEndDecelerating")
shouldLoadData = true
loadMoreData()
}
}
}
}
}
我在带有 height = UITableView.automaticDimension
的 tableView 中使用了 collectionView 并且还实现了分页。启动时没有出现额外空间,但稍后在调用 API 并重新加载 tableView 后出现。仅在重新加载 tableView 后,额外空间才会出现在最后一个单元格的末尾。
提前致谢!!!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)