- iOS 8.0+
- Xcode 9.0+
- Swift 4.0+
- CocoaPods
pod 'SLGenericsNineView'
通过泛型的思想完成九宫格布局,所有边距,大小可自定义。可传入自定义的view,自定义的model。通过map函数完成view和model的值对应关系。 只需要确定控件的位置, 其内容尺寸会自动计算, 和UILabel类似。
import SLGenericsNineView
class FrameViewController: UIViewController {
var demoView:SLGenericsNineView<CustomView,Person>!
lazy var arr:[Person] = {
return Person.getDataArr()
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
initSubviews()
}
func initSubviews(){
demoView = SLGenericsNineView(totalWidth: self.view.frame.width, map: { (view, model) in
view.nameLabel.text = model.name
view.countLabel.text = model.count
})
demoView.frame.origin = CGPoint(x: 0, y: 100)
self.view.addSubview(demoView)
demoView.dataArr = arr
}
}
import SLGenericsNineView
class AutolayoutViewController: UIViewController {
var demoView:SLGenericsNineView<CustomView,Person>!
lazy var arr:[Person] = {
return Person.getDataArr()
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
initSubviews()
}
func initSubviews(){
demoView = SLGenericsNineView(totalWidth: self.view.frame.width, map: { (view, model) in
view.nameLabel.text = model.name
view.countLabel.text = model.count
})
self.view.addSubview(demoView)
demoView.snp.makeConstraints { (make) in
make.center.equalTo(self.view)
}
demoView.dataArr = arr
}
}
//CustomXibView
var demoView:SLGenericsNineView<CustomXibView,Person>!
demoView = SLGenericsNineView(totalWidth: self.view.frame.width, map: { (view, model) in
view.nameLabel.text = model.name
view.countLabel.text = model.count
})
demoView.frame.origin = CGPoint(x: 0, y: 100)
// 声明子控件从xib中加载
demoView.isCellLoadFromXib = true
self.view.addSubview(demoView)
demoView.dataArr = arr
- 修改每行显示的个数
demoView.everyRowCount = 2
demoView.reLayoutSubViews()
- 刷新数据
demoView.reloadData()
- 点击事件监听
demoView.itemClicked = {(view, model, index) in
debugPrint(index)
debugPrint(view)
debugPrint(model)
}
- 修改边距
/// 上边距
public var topMargin:CGFloat
/// 左边距
public var leftMargin:CGFloat
/// 右边距
public var rightMargin:CGFloat
/// 下边距
public var bottomMargin:CGFloat
/// 设置边距
public func set(edges:CGFloat)
/// 水平间距
public var horizontalSpace:CGFloat
/// 垂直间距
public var verticalSpace:CGFloat