1.获取类名方法
extension NSObject{
//获取类名方法
class func classNameAsString(_ obj: Any) -> String {
//prints more readable results for dictionaries, arrays, Int, etc
return String(describing: type(of: obj))
}
}
可以扩展NSObject 全局调用
2.UITableViewCell注册cell方法
public static func cellWithTableView(tableView:UITableView)->FeedBackDetailTableViewCell{
let identify = classNameAsString(FeedBackDetailTableViewCell())
var cell: FeedBackDetailTableViewCell?
cell = tableView.dequeueReusableCell(withIdentifier: identify as String ) as? FeedBackDetailTableViewCell
if cell == nil {
cell = (Bundle.main.loadNibNamed(identify as String, owner: nil, options: nil)?.first as! FeedBackDetailTableViewCell)
}
return cell!
}
封装注册UITableViewCell的方法
extension UITableView {
/// 获取缓存池中的cell
/// - Parameter cellClass: 需要获取的cell类
/// - Returns: cell
func dequeueCellFromNib<T: UITableViewCell>(cellClass: T.Type) -> T{
var cell: T?
cell = self.dequeueReusableCell(withIdentifier: "\(cellClass)") as? T
if cell == nil {
cell = (Bundle.main.loadNibNamed("\(cellClass)", owner: nil, options: nil)?.first as? T)
}
return cell!
}
/// 获取缓存池中的cell
/// - Parameters:
/// - cellClass: 需要获取的cell类
/// - cellStyle: cellStyle 默认default
/// - Returns: cell
func dequeueCell<T: UITableViewCell>(cellClass: T.Type, _ cellStyle: T.CellStyle? = .default) -> T{
var cell: T?
cell = self.dequeueReusableCell(withIdentifier: "\(cellClass)") as? T
if cell == nil {
cell = T.init(style: cellStyle!, reuseIdentifier: "\(cellClass)")
}
return cell!
}
}
调用注册cell方法
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//调用注册cell方法
let cell = tableView.dequeueCellFromNib(cellClass: FeedBackDetailTableViewCell.self)
cell.selectionStyle = .none//设置cell点击效果
guard viewModel.dataArray.count > indexPath.row else {
return cell
}
cell.feedBackDetailModel = viewModel.dataArray[indexPath.row]
return cell
}
3.Swift 九宫格
let xa : CGFloat = 0 //x 初始值
let ya : CGFloat = 0 //y 初始值
let xz : CGFloat = 5//横向间隔
let yz : CGFloat = 10 //纵向间隔
let Width = (ScreenWidth-15*2-xz*3)/4
let Height = (ScreenWidth-15*2-xz*3)/4
for i in 0..<imageArray.count {
let X = xa+(xz + Width)*CGFloat((i % 4))
let Y = ya+(yz + Height)*CGFloat((i / 4))
let imageView = UIImageView(frame:CGRect(x:X, y:Y, width:Width, height:Height))
let imageStr = imageArray[i] as! String
imageView.kf.setImage(with: KfnisherImageResource(urlString:imageStr))
photoView.addSubview(imageView)
}
4.Swift 判断 / %
两数相除或者取余之前都需转成Int类型
let chu = (images?.count ?? 0) / 4 //整除
let yu = (images?.count ?? 0) % 4 //取余
let rowHeight = Int((ScreenWidth-15*2-xz*3)/4) //图片宽度高度
let lineHeight = 10 //上下图片间距
if chu == 0 { //两数相除除数为0 则为未被整除如2/4则只有一行
make.height.equalTo(rowHeight+lineHeight)
}else{
if yu == 0 { //两数相除余数为0 则为被整除如4/4或者8/4
make.height.equalTo((rowHeight+lineHeight)*chu+lineHeight)
}else{ //两数相除余数不为0 则为被整除如6/4或者7/4
make.height.equalTo((rowHeight+lineHeight)*chu+rowHeight+lineHeight)
}
}
5.Swift SnapKit更新约束
更新约束之前需要先移除之前的约束,再重新设置约束
codeView.snp.removeConstraints()
codeView.snp.makeConstraints { (make) in
make.top.equalTo(phoneView.snp.bottom).offset(15)
make.height.equalTo(45)
}
6.Swift根据是否是刘海屏判断iphone X
// NavBarHeight
let NavBarHeight : CGFloat = isIPhoneXSeries ? 88 : 64
// TabBarHeight
let TabBarHeight : CGFloat = isIPhoneXSeries ? 49 + 34 : 49
////Mark ----- 是否为iPhone X等全屏手机
public var isIPhoneXSeries: Bool {
var iPhoneXSeries = false
if UIDevice.current.userInterfaceIdiom != UIUserInterfaceIdiom.phone {
return iPhoneXSeries
}
if #available(iOS 11.0, *) {
if let bottom = UIApplication.shared.delegate?.window??.safeAreaInsets.bottom {
if bottom > CGFloat(0.0) {
iPhoneXSeries = true
}
}
}
return iPhoneXSeries
}
网友评论