UITableView上下轮播
2018-06-09 本文已影响44人
白色天空729
思路借鉴demo,我索性改造成了swift版本的。
https://github.com/login?return_to=%2Fzhouxubin%2FXBLoopLabel
具体调用代码:
class ThirdVC: UIViewController {
var dataSouece : [String] = ["111","3333","dasd"]
var loopV : TextLoopV!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
loopV = TextLoopV.textLoopView(with: dataSouece, loopInterval: 3.0, initWithFrame: CGRect.init(x: 0, y: 100, width: 300, height: 100)) { (a, ind) in
prints(a,ind)
}
view.addSubview(loopV)
}
}
实现代码:
class TextLoopV: UIView,UITableViewDelegate,UITableViewDataSource {
/// 定义闭包
typealias sendValueClosure = (_ sendString: String,_ index : Int) -> Void//声明
/// 回调
var callBackString: sendValueClosure?//持有
/// 数据源
var dataSource : [String] = [String]()
/// 表格
var tableView : UITableView!
/// 累加索引
var currentRowIndex : Int = 0
/// 自动几秒时间
var interval : TimeInterval {
get {
return 1.0
}
set {
let timer = Timer.scheduledTimer(timeInterval: interval, target: self, selector: #selector(timerStuff), userInfo: nil, repeats: true)
self.timer = timer
}
}
var timer : Timer!
/// 定时器做的事
@objc private func timerStuff() {
prints(self.currentRowIndex)
self.currentRowIndex += 1
self.tableView.setContentOffset(CGPoint.init(x: 0, y: CGFloat(self.currentRowIndex) * self.tableView.rowHeight), animated: true)
}
/// 初始化上下滚动的视图
///
/// - Parameters:
/// - dataSource: 数据源
/// - timeInterval: 时间间隔
/// - frame: 容器大小
/// - selectBlock: 传回的闭包
/// - Returns: 返回本身
class func textLoopView(with dataSource: [String], loopInterval timeInterval: TimeInterval, initWithFrame frame: CGRect, select selectBlock: @escaping sendValueClosure) -> TextLoopV {
let loopV = TextLoopV.init(frame: frame)
loopV.dataSource = dataSource
loopV.interval = timeInterval
loopV.callBackString = selectBlock
return loopV
}
override init(frame: CGRect) {
super.init(frame: frame)
self.tableView = UITableView.init(frame: CGRect.init(x: 0, y: 0, width: frame.width, height: frame.height))
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.separatorStyle = .none
self.tableView.layer.borderWidth = 1
self.tableView.rowHeight = frame.height
self.tableView.zdx_register(TextLoopCell.classForCoder())
self.addSubview(self.tableView)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//MARK:scrollView代理
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
if self.currentRowIndex == self.dataSource.count {
self.currentRowIndex = 0
self.tableView.setContentOffset(CGPoint.zero, animated: false)
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if self.callBackString != nil {
self.callBackString!(self.dataSource[indexPath.row],indexPath.row)
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.zdx_dequeueReusableCell(indexPath: indexPath) as TextLoopCell
cell.ddd.text = self.dataSource[indexPath.row]
return cell
}
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.dataSource.count
}
}
自定义的表格单元格cell:
class TextLoopCell: CommonTableViewCell {
var ddd : UILabel!
var baseIMg : BaseUIImageView!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: .default, reuseIdentifier: reuseIdentifier)
ddd = UILabel.init(frame: frame)
baseIMg = BaseUIImageView.init(CGRect.init(x: frame.midX, y: frame.midY, width: frame.width / 2, height: frame.height / 2), #imageLiteral(resourceName: "default"))
contentView.addSubview(ddd)
contentView.addSubview(baseIMg)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
1.有几个注意点:oc 定义变量后,会默认持有setter和getter方法。
swift对应的是set 、get。
https://www.jianshu.com/p/87fc570e44df
2.再则,就是swift中闭包的定义注意。
https://www.jianshu.com/p/5a3fd872257e