Skip to content

Commit 039a028

Browse files
committed
创建Swift项目模板
1.网络请求 2.HUD 3.Table协议 4.JSON->Model
1 parent 2331dd6 commit 039a028

File tree

82 files changed

+2979
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+2979
-0
lines changed

.gitignore

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
# Xcode
3+
#
4+
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
5+
6+
## Build generated
7+
build/
8+
DerivedData/
9+
10+
## Various settings
11+
*.pbxuser
12+
!default.pbxuser
13+
*.mode1v3
14+
!default.mode1v3
15+
*.mode2v3
16+
!default.mode2v3
17+
*.perspectivev3
18+
!default.perspectivev3
19+
xcuserdata/
20+
21+
## Other
22+
*.moved-aside
23+
*.xccheckout
24+
*.xcscmblueprint
25+
26+
## Obj-C/Swift specific
27+
*.hmap
28+
*.ipa
29+
*.dSYM.zip
30+
*.dSYM
31+
32+
## Playgrounds
33+
timeline.xctimeline
34+
playground.xcworkspace
35+
36+
# Swift Package Manager
37+
#
38+
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
39+
# Packages/
40+
# Package.pins
41+
# Package.resolved
42+
.build/
43+
44+
# CocoaPods
45+
#
46+
# We recommend against adding the Pods directory to your .gitignore. However
47+
# you should judge for yourself, the pros and cons are mentioned at:
48+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
49+
#
50+
# Pods/
51+
Pods/
52+
Podfile.lock
53+
*.xcworkspace/
54+
*.DS_Store
55+
56+
# Carthage
57+
#
58+
# Add this line if you want to avoid checking in source code from Carthage dependencies.
59+
# Carthage/Checkouts
60+
61+
Carthage/Build
62+
63+
# fastlane
64+
#
65+
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
66+
# screenshots whenever they are needed.
67+
# For more information about the recommended setup visit:
68+
# https://docs.fastlane.tools/best-practices/source-control/#source-control
69+
70+
fastlane/report.xml
71+
fastlane/Preview.html
72+
fastlane/screenshots/**/*.png
73+
fastlane/test_output

Podfile

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Uncomment this line to define a global platform for your project
2+
3+
source 'https://github.com/CocoaPods/Specs.git'
4+
platform :ios, '9.0'
5+
use_frameworks!
6+
7+
def pods
8+
pod 'Alamofire'
9+
pod 'Kingfisher'
10+
pod 'SnapKit'
11+
pod 'MJRefresh'
12+
pod 'PKHUD'
13+
pod 'ObjectMapper'
14+
pod 'SwiftyJSON'
15+
end
16+
17+
18+
19+
target 'SwiftTemplate' do
20+
pods
21+
22+
end

README.md

+68
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,70 @@
11
# ProjectTemplate
22
这是一个Swift项目模板,包含了TableView基本重用协议、JSON转Model、网络API统一封装处理
3+
4+
## 步骤
5+
- 将Library/Modules/Resource三个文件夹,Podfile 文件拖到项目中.
6+
- info.plis位置修改 路径改到 ProjectName/Resource/info.plist
7+
- 删除copy的info.plist。路径:TARGETS---Build Phases --- Copy Bundle Resource.
8+
- 修改 Podfile 文件中的 `SwiftTemplate` 为你的项目名`Your ProjectName`
9+
- 执行`pod install`安装库,运行项目
10+
```bash
11+
# 由于我用的是HTTP,需要在 info.plist 配置如下Key才能请求数据
12+
<key>NSAppTransportSecurity</key>
13+
<dict>
14+
<key>NSAllowsArbitraryLoads</key>
15+
<true/>
16+
</dict>
17+
</plist>
18+
19+
``
20+
21+
## 项目结构
22+
23+
```bash
24+
├── Library #这个看名称就知道
25+
│   ├── ClassExtension # 扩展
26+
│   ├── Project # 项目HUD封装
27+
│   ├── Protocol # 协议
28+
│   ├── Refresh # Refresh
29+
│   └── Utils # 公共工具
30+
├── Modules
31+
│   ├── Base # 基类模块
32+
│   ├── Launching # 启动模块,UI跳转管理
33+
│   ├── Network # 网络API封装模块
34+
│   ├── ModuleA # 你的业务内容模块A
35+
│   └── ModuleB # 你的业务内容模块B
36+
└── Resource
37+
├── Assets.xcassets # 图片资源
38+
├── Base.lproj
39+
└── Info.plist
40+
```
41+
42+
### TableView
43+
```swift
44+
lazy var tableView: UITableView = {[unowned self] in
45+
let tableView = TableViewFactory.createTableView(self)
46+
self.view.addSubview(tableView)
47+
tableView.snp_makeConstraints { (make) in
48+
make.edges.equalToSuperview()
49+
}
50+
tableView.registerClassOf(UITableViewCell.self)
51+
LTRefresh.addHeaderRefresh(to: tableView, target: self, action: #selector(getNewsData))
52+
return tableView
53+
}()
54+
```
55+
56+
```swift
57+
extension ViewController: UITableViewDataSource, UITableViewDelegate {
58+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
59+
return dataList?.count ?? 0
60+
}
61+
62+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
63+
let cell: UITableViewCell = tableView.dequeueReusableCell()
64+
cell.textLabel?.text = dataList![indexPath.row].name
65+
return cell
66+
}
67+
68+
}
69+
```
70+
[img](screen.png)

SwiftTemplate.xcodeproj/project.pbxproj

+584
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//
2+
// Date+Extension.swift
3+
// LinkTower
4+
//
5+
// Created by Hanson on 2018/6/5.
6+
// Copyright © 2018 Hanson. All rights reserved.
7+
//
8+
9+
import Foundation
10+
extension Date {
11+
12+
static func getCurrentText(format: String) -> String {
13+
return Date().getText(format: format)
14+
}
15+
16+
static func getCurrentWeakDay() -> String {
17+
return getWeakDay(for: Date())
18+
}
19+
20+
21+
22+
static func getWeakDay(for date: Date) -> String {
23+
24+
let calendar: NSCalendar = NSCalendar.current as NSCalendar
25+
let unitFlags: NSCalendar.Unit = [.year, .month, .day, .weekday, .hour]
26+
let components = calendar.components(unitFlags, from: date)
27+
guard let weekday = components.weekday else {
28+
return ""
29+
}
30+
switch weekday {
31+
case 1:
32+
return "星期天"
33+
case 2:
34+
return "星期一"
35+
case 3:
36+
return "星期二"
37+
case 4:
38+
return "星期三"
39+
case 5:
40+
return "星期四"
41+
case 6:
42+
return "星期五"
43+
case 7:
44+
return "星期六"
45+
default:
46+
return ""
47+
}
48+
}
49+
50+
static func getText(from date: Date, format: String) -> String {
51+
return date.getText(format: format)
52+
}
53+
54+
public func getText(format: String) -> String {
55+
let dateFormatter = DateFormatter()
56+
dateFormatter.dateFormat = format
57+
return dateFormatter.string(from: self)
58+
}
59+
60+
static func getDate(from date: String, format: String = "yyyy/MM/dd HH:mm:ss")
61+
-> Date? {
62+
let dateFormatter = DateFormatter()
63+
dateFormatter.dateFormat = format
64+
return dateFormatter.date(from: date)
65+
}
66+
}
67+
68+
extension String {
69+
public func toDate(_ format: String = "yyyy/MM/dd HH:mm:ss") -> Date? {
70+
let dateFormatter = DateFormatter()
71+
dateFormatter.dateFormat = format
72+
return dateFormatter.date(from: self)
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// TableView+Extension.swift
3+
// Hanson
4+
//
5+
// Created by Hanson on 9/7/17.
6+
// Copyright © 2017 Hanson. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
extension UITableView {
12+
13+
func registerClassOf<T: UITableViewCell>(_: T.Type) {
14+
register(T.self, forCellReuseIdentifier: T.reuseIdentifier)
15+
}
16+
17+
func registerNibOf<T: UITableViewCell>(_: T.Type) {
18+
let nib = UINib(nibName: T.nibName, bundle: nil)
19+
register(nib, forCellReuseIdentifier: T.reuseIdentifier)
20+
}
21+
22+
func registerHeaderFooterClassOf<T: UITableViewHeaderFooterView>(_: T.Type) {
23+
register(T.self, forHeaderFooterViewReuseIdentifier: T.reuseIdentifier)
24+
}
25+
26+
func dequeueReusableCell<T: UITableViewCell>() -> T {
27+
guard let cell = dequeueReusableCell(withIdentifier: T.reuseIdentifier) as? T else {
28+
fatalError("Could not dequeue cell with identifier: \(T.reuseIdentifier)")
29+
}
30+
31+
return cell
32+
}
33+
34+
func dequeueReusableHeaderFooter<T: UITableViewHeaderFooterView>() -> T {
35+
guard let view = dequeueReusableHeaderFooterView(withIdentifier: T.reuseIdentifier) as? T else {
36+
fatalError("Could not dequeue HeaderFooter with identifier: \(T.reuseIdentifier)")
37+
}
38+
39+
return view
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// UICollectionView+Extension.swift
3+
// YiBiFen_CN
4+
//
5+
// Created by Hanson on 28/12/2017.
6+
// Copyright © 2017 hhly. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
extension UICollectionView {
12+
func registerClassOf<T: UICollectionViewCell>(_: T.Type) {
13+
register(T.self, forCellWithReuseIdentifier: T.reuseIdentifier)
14+
}
15+
16+
func registerClassOf<T: UICollectionReusableView>(_: T.Type, elementKind: String) {
17+
register(T.self, forSupplementaryViewOfKind: elementKind, withReuseIdentifier: T.reuseIdentifier)
18+
}
19+
20+
func registerNibOf<T: UICollectionViewCell>(_: T.Type) {
21+
22+
let nib = UINib(nibName: T.nibName, bundle: nil)
23+
register(nib, forCellWithReuseIdentifier: T.reuseIdentifier)
24+
}
25+
26+
27+
func dequeueReusableSupplementaryView<T: UICollectionReusableView>(elementKind: String, indexPath: IndexPath)
28+
-> T {
29+
guard let cell = dequeueReusableSupplementaryView(ofKind: elementKind, withReuseIdentifier: T.reuseIdentifier, for: indexPath) as? T else {
30+
fatalError("Could not dequeue cell with identifier: \(T.reuseIdentifier)")
31+
}
32+
return cell
33+
}
34+
35+
func dequeueReusableCell<T: UICollectionViewCell>(indexPath: IndexPath) -> T {
36+
37+
guard let cell = dequeueReusableCell(withReuseIdentifier: T.reuseIdentifier, for: indexPath) as? T else {
38+
fatalError("Could not dequeue cell with identifier: \(T.reuseIdentifier)")
39+
}
40+
return cell
41+
}
42+
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// UIColor+Extension.swift
3+
// YiBiFen
4+
//
5+
// Created by Hanson on 16/10/13.
6+
// Copyright © 2016年 Hanson. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
extension UIColor {
12+
//用数值初始化颜色,便于生成设计图上标明的十六进制颜色
13+
convenience init(hex hexText: UInt) {
14+
self.init(
15+
red: CGFloat((hexText & 0xFF0000) >> 16) / 255.0,
16+
green: CGFloat((hexText & 0x00FF00) >> 8) / 255.0,
17+
blue: CGFloat(hexText & 0x0000FF) / 255.0,
18+
alpha: CGFloat(1.0)
19+
)
20+
}
21+
22+
convenience init(red: CGFloat, green: CGFloat, blue: CGFloat) {
23+
self.init( red: red / 255.0, green: green / 255.0,
24+
blue: blue / 255.0, alpha: 1.0 )
25+
}
26+
27+
public class var random: UIColor {
28+
get {
29+
let red = CGFloat(arc4random() % 256) / 255.0
30+
let green = CGFloat(arc4random() % 256) / 255.0
31+
let blue = CGFloat(arc4random() % 256) / 255.0
32+
return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
33+
}
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// UIImage+Extension.swift
3+
// LinkTower
4+
//
5+
// Created by Hanson on 2018/6/16.
6+
// Copyright © 2018年 Hanson. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
public extension String {
12+
13+
var image: UIImage? {
14+
return UIImage(named: self)
15+
}
16+
17+
var templateImage: UIImage? {
18+
return UIImage(named: "\(self)_select")
19+
}
20+
21+
var originalImage: UIImage? {
22+
return UIImage(named: "\(self)_normal")
23+
}
24+
}

0 commit comments

Comments
 (0)