table 视图单元格中的滚动视图无法正确显示图像并且在 swift 4 中的分页模式下有一个额外的页面
scroll view in table view cell won't show correctly images and there is one extra page in paging mode in swift 4
我在 table 视图单元格中使用带有图像的滚动视图但是有两个问题:
1- 有一个额外的页面不包括我的图像和
2:图片没有填满屏幕,每张图片之间有白色space
这是我的 table 带有滚动视图代码的视图单元格
class scrollViewCell: UITableViewCell , UIScrollViewDelegate {
@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var scrollView: UIScrollView!
var contentWidth : CGFloat = 0.0
override func awakeFromNib() {
super.awakeFromNib()
scrollView.delegate = self
for image in 0...2 {
let imageToDisplay = UIImage(named : "1")
let imageView = UIImageView(image : imageToDisplay)
let xCoordinate = self.contentView.frame.midX + self.contentView.frame.width * CGFloat(image)
contentWidth += self.contentView.frame.width
scrollView.addSubview(imageView)
imageView.frame = CGRect(x: xCoordinate - 50 , y: 0 , width: self.contentView.frame.width, height: 100)
imageView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleBottomMargin, .flexibleRightMargin, .flexibleLeftMargin, .flexibleTopMargin]
imageView.contentMode = .scaleAspectFit // OR .scaleAspectFill
imageView.clipsToBounds = true
}
scrollView.contentSize = CGSize(width: contentWidth, height: self.contentView.frame.height)
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
pageControl.currentPage = Int(scrollView.contentOffset.x / CGFloat(414))
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
问题出在您的 xCoordinate 计算中,contentView 框架在 awakeFromNib 中不可用。
您不应该使用 super.awakeFromNib
来计算帧数。
创建一个名为
的方法
func configureUI(){
//Update your UI here
}
从 cellForRowAtIndex:
UITableViewCell 的委托方法中调用此方法
你能试试这个吗
func addscrollV()
{
for image in 0...2 {
let imageToDisplay = UIImage(named : "1")
let imageView = UIImageView(image : imageToDisplay)
let xCoordinate = self.contentView.frame.width * CGFloat(image)
contentWidth += self.contentView.frame.width
scrollView.addSubview(imageView)
imageView.frame = CGRect(x: xCoordinate , y: 0 , width: self.contentView.frame.width, height: self.contentView.frame.height)
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
}
scrollView.contentSize = CGSize(width: contentWidth, height: self.contentView.frame.height)
}
func layoutSubviews()
{
super.layoutSubviews()
if(Once)
{
Once = false
self.addscrollV()
}
}
我在 table 视图单元格中使用带有图像的滚动视图但是有两个问题:
1- 有一个额外的页面不包括我的图像和
2:图片没有填满屏幕,每张图片之间有白色space
这是我的 table 带有滚动视图代码的视图单元格
class scrollViewCell: UITableViewCell , UIScrollViewDelegate {
@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var scrollView: UIScrollView!
var contentWidth : CGFloat = 0.0
override func awakeFromNib() {
super.awakeFromNib()
scrollView.delegate = self
for image in 0...2 {
let imageToDisplay = UIImage(named : "1")
let imageView = UIImageView(image : imageToDisplay)
let xCoordinate = self.contentView.frame.midX + self.contentView.frame.width * CGFloat(image)
contentWidth += self.contentView.frame.width
scrollView.addSubview(imageView)
imageView.frame = CGRect(x: xCoordinate - 50 , y: 0 , width: self.contentView.frame.width, height: 100)
imageView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleBottomMargin, .flexibleRightMargin, .flexibleLeftMargin, .flexibleTopMargin]
imageView.contentMode = .scaleAspectFit // OR .scaleAspectFill
imageView.clipsToBounds = true
}
scrollView.contentSize = CGSize(width: contentWidth, height: self.contentView.frame.height)
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
pageControl.currentPage = Int(scrollView.contentOffset.x / CGFloat(414))
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
问题出在您的 xCoordinate 计算中,contentView 框架在 awakeFromNib 中不可用。
您不应该使用 super.awakeFromNib
来计算帧数。
创建一个名为
func configureUI(){
//Update your UI here
}
从 cellForRowAtIndex:
UITableViewCell 的委托方法中调用此方法
你能试试这个吗
func addscrollV()
{
for image in 0...2 {
let imageToDisplay = UIImage(named : "1")
let imageView = UIImageView(image : imageToDisplay)
let xCoordinate = self.contentView.frame.width * CGFloat(image)
contentWidth += self.contentView.frame.width
scrollView.addSubview(imageView)
imageView.frame = CGRect(x: xCoordinate , y: 0 , width: self.contentView.frame.width, height: self.contentView.frame.height)
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
}
scrollView.contentSize = CGSize(width: contentWidth, height: self.contentView.frame.height)
}
func layoutSubviews()
{
super.layoutSubviews()
if(Once)
{
Once = false
self.addscrollV()
}
}