var data:nsarray!
var scrollview:uiscrollview!
var pagectrl:uipagecontrol!
Override Func Viewdidload () {
Super.viewdidload ()
Instantiate TableView
Let Tableview:uitableview = UITableView (Frame:self.view.bounds, Style:UITableViewStyle.Plain)
Tableview.delegate = Self
Tableview.datasource = Self
Tableview.registerclass (uitableviewcell.self, Forcellreuseidentifier: "Cell")
Self.view.addSubview (TableView)
System font
data = Uifont.familynames ()
Instantiate ScrollView
Let Widthm = TableView.frame.size.width
Let View:uiview = UIView (Frame:cgrect (x:0, y:0, width:0, height:200))
ScrollView = Uiscrollview (Frame:cgrect (x:0, y:0, Width:widthm, height:200))
Scrollview.contentsize = Cgsize (Width:widthm * 5, height:200)
Scrollview.showshorizontalscrollindicator = False
Scrollview.showsverticalscrollindicator = False
Scrollview.pagingenabled = True
Scrollview.delegate = Self
For index in 1...5 {
Let imgname = "image\ (index). jpg"
Let Imgview:uiimageview = Uiimageview (Image:uiimage (named:imgname))
Imgview.frame = CGRect (X:widthm * cgfloat (index-1), y:0, Width:widthm, height:200)
Scrollview.addsubview (Imgview)
}
View.autoresizessubviews = False
View.addsubview (ScrollView)
Instantiating a paging controller
PageCtrl = Uipagecontrol (Frame:cgrect (x:0, y:200-20, Width:widthm, height:20))
Pagectrl.addtarget (Self, Action:selector ("Pagectrlaction:"), forControlEvents:UIControlEvents.ValueChanged)
Pagectrl.numberofpages = 5
Pagectrl.currentpage = 0
View.addsubview (PageCtrl)
Tableview.tableheaderview = view
}
Mark:uitableviewdatasource
Func TableView (Tableview:uitableview, Numberofrowsinsection section:int), Int {
Return Data.count
}
Func TableView (Tableview:uitableview, Cellforrowatindexpath indexpath:nsindexpath), UITableViewCell {
Let Tableviewcell = Tableview.dequeuereusablecellwithidentifier ("cell", Forindexpath:indexpath) as UITableViewCell
Tableviewcell.textlabel?. Text = Self.data[indexpath.row] as? String
Return Tableviewcell
}
Mark:uiscrollviewdelegate
Func scrollviewdidenddecelerating (Scrollview:uiscrollview) {
if ScrollView = = Self.scrollview {
Pagectrl.currentpage = Int (scrollview.contentoffset.x/scrollview.frame.size.width)
}
}
Mark:pagectrl Action
Func pagectrlaction (Pagectrl:uipagecontrol) {
Scrollview.contentoffset.x = ScrollView.frame.width * CGFloat (Pagectrl.currentpage)
Scrollview.setcontentoffset (Cgpoint (X:scrollview.frame.width * cgfloat (pagectrl.currentpage), y:0), animated:true)
}
Swift Page Paging view