我正在尝试将UIScrollView()
添加到我的一个表格单元格中。我很难搞清楚用什么方法来放置滚动视图的代码。
我在我的应用中的其他地方有一个滚动视图,这是我使用的代码转移到我的UITableView()
视图,因为我猜应该是:
var page = Int()
var pageViews: [UIImageView?] = []
var sixPlusImages = [
UIImage(named: "Glance 5.5"),
UIImage(named: "Currencies 5.5"),
UIImage(named: "Swipe 5.5"),
UIImage(named: "Format 5.5"),
UIImage(named: "Search 5.5")
]
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:AppImagesTableViewCell = self.appTableView.dequeueReusableCellWithIdentifier("appImages", forIndexPath: indexPath) as! AppImagesTableViewCell
return cell
}
func loadPage(page: Int) {
let cell = appTableView.cellForRowAtIndexPath(NSIndexPath(index: 0))
if page < 0 || page >= pageViews.count {
return
}
if let pageView = pageViews[page] {
return
} else {
var imageFrame = CGRect(x: (cell.scrollView.bounds.width * CGFloat(page)) + 5, y: 0, width: (cell.scrollView.bounds.width) - 10, height: 40)
let imageView = UIImageView()
imageView.contentMode = .ScaleAspectFit
imageView.frame = imageFrame
imageView.image = sixPlusImages[page]
cell.scrollView.addSubview(imageView)
}
pageViews[page] = imageView
}
func purgePage(page: Int) {
if page < 0 || page >= sixPlusImages.count {
return
}
if let pageView = pageViews[page] {
pageView.removeFromSuperview()
pageViews[page] = nil
}
}
func loadVisiblePages() {
let pageWidth = cell.scrollView.frame.size.width
page = Int(floor((cell.scrollView.contentOffset.x * 2.0 + pageWidth) / (pageWidth * 2.0)))
pageControl.currentPage = page
let firstPage = page - 1
let lastPage = page + 1
for var index = 0; index < firstPage; ++index {
purgePage(index)
}
for index in firstPage...lastPage {
loadPage(index)
}
for var index = lastPage+1; index < sixPlusImages.count; ++index {
purgePage(index)
}
}
func scrollViewDidScroll(scrollView: UIScrollView) {
loadVisiblePages()
}
显然,它有很多问题。我似乎无法访问我在单元类中作为插座的scrollView,这会导致一些问题。
有人能给我一些指导吗?我在网上找不到任何Swift示例。
感谢。
答案 0 :(得分:2)
我注意到您正在使用UIScrollView Tutorial: Getting Started教程中建议的部分代码来使用UIScrollView
制作幻灯片。
您可以在自定义UIScrollView
内使用UITableViewCell
而不会有任何问题,首先您必须在@IBOutlet
内声明CustomTableViewCell
。
类CustomTableViewCell
必须实现协议UIScrollViewDelegate
才能通知它UIScrollView
中的更改,您必须实现处理其中UIScrollView
的函数。我们来看看下面的例子:
CustomTableViewCell 来处理
UIScrollView
class CustomTableViewCell: UITableViewCell, UIScrollViewDelegate {
@IBOutlet weak var scrollView: UIScrollView!
var pageImages: [UIImage] = []
var pageViews: [UIImageView?] = []
override func awakeFromNib() {
super.awakeFromNib()
// 1
pageImages = [UIImage(named: "photo1.jpg")!,
UIImage(named: "photo2.jpg")!,
UIImage(named: "photo3.jpg")!]
let pageCount = pageImages.count
// 3
for _ in 0..<pageCount {
pageViews.append(nil)
}
// 4
let pagesScrollViewSize = scrollView.frame.size
scrollView.contentSize = CGSize(width: pagesScrollViewSize.width * CGFloat(pageImages.count),
height: pagesScrollViewSize.height)
// 5
loadVisiblePages()
}
func loadPage(page: Int) {
if page < 0 || page >= pageImages.count {
// If it's outside the range of what you have to display, then do nothing
return
}
// 1
if let pageView = pageViews[page] {
// Do nothing. The view is already loaded.
} else {
// 2
var frame = scrollView.bounds
frame.origin.x = frame.size.width * CGFloat(page)
frame.origin.y = 0.0
// 3
let newPageView = UIImageView(image: pageImages[page])
newPageView.contentMode = .ScaleAspectFit
newPageView.frame = frame
scrollView.addSubview(newPageView)
// 4
pageViews[page] = newPageView
}
}
func loadVisiblePages() {
// First, determine which page is currently visible
let pageWidth = scrollView.frame.size.width
let page = Int(floor((scrollView.contentOffset.x * 2.0 + pageWidth) / (pageWidth * 2.0)))
// Work out which pages you want to load
let firstPage = page - 1
let lastPage = page + 1
// Purge anything before the first page
for var index = 0; index < firstPage; ++index {
purgePage(index)
}
// Load pages in our range
for index in firstPage...lastPage {
loadPage(index)
}
// Purge anything after the last page
for var index = lastPage+1; index < pageImages.count; ++index {
purgePage(index)
}
}
func scrollViewDidScroll(scrollView: UIScrollView) {
// Load the pages that are now on screen
loadVisiblePages()
}
func purgePage(page: Int) {
if page < 0 || page >= pageImages.count {
// If it's outside the range of what you have to display, then do nothing
return
}
// Remove a page from the scroll view and reset the container array
if let pageView = pageViews[page] {
pageView.removeFromSuperview()
pageViews[page] = nil
}
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
然后在您cellForRowAtIndexPath
或UITableViewController
的{{1}}中,您唯一需要的是以下代码:
ViewController
以上代码仅作为示例,您必须考虑override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomTableViewCell
return cell
}
的约束,启用分页启用等。
我希望这对你有所帮助。