我是個(gè)swift初探者,至于我為什么稱(chēng)這個(gè)工程為半swift工程,因?yàn)檫@個(gè)工程并不是我一點(diǎn)點(diǎn)用swift寫(xiě)出來(lái)的,而是我將我的oc代碼一點(diǎn)點(diǎn)的轉(zhuǎn)化為swift,而且也不涉及特別復(fù)雜的邏輯,只有一個(gè)tableView展示,過(guò)程中也遇到了一些困難,比如swift的懶加載,swift的閉包,swift的泛型數(shù)組,swift的聲明等等,對(duì)于一個(gè)初探者,都是一點(diǎn)點(diǎn)摳出來(lái)的。好了不多說(shuō)直接上代碼,至于我說(shuō)的哪些困難,在工程里也有備注。
工程的github網(wǎng)址https://github.com/yuyuepeng/tableViewTry 下載后需要重新設(shè)置橋接文件的路徑,方法如圖,可以直接橋接文件FirstTryHeader.h直接拖過(guò)去,大家哪有什么看不明白的可以在文章下面留言哦。

AppDelegate不多說(shuō)
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow.init(frame:UIScreen.main.bounds)
let vc:ViewController = ViewController.init()
let bc:UINavigationController = UINavigationController.init(rootViewController: vc)
self.window?.rootViewController = bc
self.window?.makeKeyAndVisible()
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
AFNetworking的二次封裝
關(guān)于這點(diǎn)涉及到oc和swift的混編 需要在target的build setting里的swift Compiler設(shè)置Objective-c Bridging Header ,方法自行百度。
import UIKit
//網(wǎng)絡(luò)封裝afn
class NetManager: NSObject {
// 懶加載
lazy var netManager:AFHTTPSessionManager = {
var netManager = AFHTTPSessionManager.init()
var set:NSSet = NSSet.init(objects: "application/json","text/html","text/json","text/javascript","text/plain")
netManager.responseSerializer.acceptableContentTypes = set as? Set<String>
return netManager
}()
// 回調(diào)方法閉包返回?cái)?shù)據(jù)和結(jié)果
typealias finished = (_ response:AnyObject?, _ result:String?) ->Void
override init() {
}
// post請(qǐng)求
func post(domain:String,path:String,form:NSDictionary,parameters:NSDictionary,finished: @escaping finished) -> Void {
var url:String = String()
// 拼接url方法
url = self.pinjie(domain: domain, path: path, parameters: parameters)
netManager.post(url as String, parameters: nil, constructingBodyWith: {(_ formData: AFMultipartFormData) -> Void in
// 上傳表單的參數(shù)
self.generate(formData: formData, data: form)
}, progress: {(_ uploadProgress: Progress) -> Void in
}, success: {(_ task: URLSessionDataTask, _ responseObject: Any) -> Void in
// 以下這句代碼可以根據(jù)公司的數(shù)據(jù)結(jié)構(gòu)文檔修改,是獲取我請(qǐng)求的url的返回的數(shù)據(jù)
var dict:NSDictionary = NSDictionary.init(dictionary: responseObject as! NSDictionary)
if (dict.object(forKey: "data") != nil) {
var arr:NSArray = dict["data"] as! NSArray
finished(arr,"成功")
}else {
finished(["11","11"] as AnyObject,"失敗")
}
}, failure: {(_ task: URLSessionDataTask?, _ error: Error) -> Void in
finished(["11","11"] as AnyObject,"失敗")
})
}
// 拼接url
func pinjie(domain:String,path:String,parameters:NSDictionary) -> String {
var url = "\(domain)"
if !path.isEmpty {
url += path
}
if parameters.allKeys.count != 0 {
if parameters.allKeys[0] as! String == "" {
}else {
url += "?"
for key: String in parameters.allKeys as! [String] {
var value = (parameters.value(forKey: key) as! String)
url += "\(key)=\(value)&"
}
url = "\(url as NSString).substring(with: NSRange(location: 0, length: url.characters.count - 1))"
}
}
return url
// var url = "\(domain)"
// url += path
//
// url += "?"
//
// for key: String in parameters.allKeys as! [String]{
// var value = (parameters.value(forKey: key) as! String)
// url += "\(key)=\(value)&"
// }
// url = "\(url as NSString).substring(with: NSRange(location: 0, length: url.characters.count - 1))"
// return url
}
func generate(formData:AFMultipartFormData,data:NSDictionary) -> Void {
for key in data.allKeys {
// 遍歷上傳
var value = data.value(forKey: (key as? String)!)
if value is NSDictionary {
// 如果value是字典,繼續(xù)執(zhí)行該方法
self.generate(formData: formData, data: value as! NSDictionary)
}else if value is String {
let strValue = value as! String
formData.appendPart(withForm: strValue.data(using: String.Encoding.utf8)!, name: key as! String)
}else if value is NSData {
let keyStr = key as! NSString
if keyStr.contains(".") {
let arr:[String] = keyStr.components(separatedBy: ".")
if arr.last == "png" || arr.last == "jpg"||arr.last == "jpeg" || arr.last == "m4a" {
let typeArray = TShopTools.mineType(with: value as! Data!).components(separatedBy: "/")
let fileName = "\(TShopTools.uniqueString()).\(typeArray.last!)"
formData.appendPart(withFileData: value as! Data, name: arr[0], fileName: fileName, mimeType: TShopTools.mineType(with: value as! Data))
}
}else {
formData.appendPart(withForm: value as! Data, name: (key as! NSString) as String)
}
}else if value is NSArray {//value是數(shù)組
let valueArr:NSArray = value as! NSArray
if valueArr.firstObject is UIImage {//上傳圖片的數(shù)組
var imageData:NSData = NSData()
var a = 1
for image:UIImage in valueArr as! [UIImage]{
if UIImagePNGRepresentation(image) == nil {
let imageData1:Data = UIImageJPEGRepresentation(image, 1.0)!
imageData = imageData1 as NSData
}else {
let imageData2:Data = UIImagePNGRepresentation(image)!
imageData = imageData2 as NSData
}
formData.appendPart(withFileData: imageData as Data, name: key as! String, fileName: "\(TShopTools.uniqueString())\(a).jpg", mimeType: "image/jpg")
a += 1
}
}else {
// 上傳字符串?dāng)?shù)組
var i = 0
for stringValue: String in valueArr as! [String] {
print("woshi\(i)---\(stringValue)")
formData.appendPart(withForm: stringValue.data(using: String.Encoding.utf8)!, name: key as! String)
i += 1
}
}
}else if value is UIImage {//上傳單張圖片
var imageData:NSData = NSData();
let image = (value as! UIImage)
if UIImagePNGRepresentation(image) == nil {
let imageData1:Data = UIImageJPEGRepresentation(image, 1.0)!
imageData = imageData1 as NSData
}else {
let imageData2:Data = UIImagePNGRepresentation(image)!
imageData = imageData2 as NSData
}
formData.appendPart(withFileData: imageData as Data, name: key as! String, fileName: "\(TShopTools.uniqueString()).jpg", mimeType: "image/jpg")
}else {
let valueStr = value as! NSValue
let valueStr1 = value as! NSString
let valueStr3 = valueStr as! NSNumber
if valueStr.responds(to: #selector(getter: NSNumber.stringValue)) {
formData.appendPart(withForm:valueStr3.stringValue.data(using: String.Encoding.utf8)! , name: key as! String)
}else if valueStr.responds(to: #selector(NSString.data(using:))) {
formData.appendPart(withForm: valueStr1.data(using: String.Encoding.utf8.rawValue)!, name: key as! String)
}
}
}
}
}
Model類(lèi)
import UIKit
class firstModelTry: NSObject {
var pic:String = String()
var title:String = String()
}
cell類(lèi)
import UIKit
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
class firstCellTry: UITableViewCell {
// 相當(dāng)于oc的setModel
var model1 : firstModelTry!
var model : firstModelTry {
set{
self.model1 = newValue
self.nameLabel.text = self.model1.title;
self.backImage.sd_setImage(with: URL.init(string: self.model1.pic), placeholderImage: UIImage.init(named: "banner_placeHolder"))
}
get{
return self.model1
}
}
//懶加載創(chuàng)建標(biāo)題label
lazy var nameLabel: UILabel = {
var singleLength = screenWidth/640.0
var nameLabel = UILabel.init(frame: CGRect.init(x: 100 * singleLength, y: 300 * singleLength, width: 440 * singleLength, height: 80 * singleLength))
nameLabel.font = UIFont.systemFont(ofSize: 15)
nameLabel.textAlignment = NSTextAlignment.center;
nameLabel.backgroundColor = UIColor.white
return nameLabel
}()
// 懶加載創(chuàng)建背景圖
lazy var backImage:UIImageView = {
var singleLength = screenWidth/640.0
var backImage = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 400 * singleLength))
return backImage
} ()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
createSubViews()
}
fileprivate func createSubViews() {
addSubview(backImage)
self.backImage.addSubview(nameLabel)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
viewController里是一個(gè)tableView,里邊只展示了一個(gè)圖片和一個(gè)標(biāo)題,需要注意的是網(wǎng)絡(luò)封裝只針對(duì)于我現(xiàn)在請(qǐng)求的這個(gè)接口,若是請(qǐng)求其他接口,請(qǐng)根據(jù)公司的接口文檔自行修改。
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{
// tableView
var tableView:UITableView = UITableView.init(frame: CGRect.init(x: 0, y: 0, width: 414, height: UIScreen.main.bounds.size.height), style: UITableViewStyle.plain)
// 數(shù)據(jù)源(一個(gè)firstModelTry類(lèi)型的數(shù)組)
var dataSource:[firstModelTry] = Array()
// 單位長(zhǎng)度
var singleLength:CGFloat = 0.0
// 網(wǎng)絡(luò)請(qǐng)求工具
var manager:NetManager = NetManager()
override func viewDidLoad() {
super.viewDidLoad()
self.automaticallyAdjustsScrollViewInsets = true;
singleLength = UIScreen.main.bounds.size.width/640.0
self.view.addSubview(tableView)
self.title = "我的第一個(gè)swifttableView"
tableView.delegate = self
tableView.dataSource = self
// 加載數(shù)據(jù)源
self.loadData()
// Do any additional setup after loading the view, typically from a nib.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.dataSource.count;
}
func loadData() -> Void {
manager.post(domain: "http://appapi.yx.dreamore.com/index/banners", path: "", form: ["":""], parameters: ["":""]) { (_ response: AnyObject?, message: String?) -> Void in
// 接請(qǐng)求下來(lái)的數(shù)組
let arr3:NSArray = response as! NSArray
for dict3:NSDictionary in arr3 as! [NSDictionary]{
// 遍歷得到Model
var model3 = firstModelTry()
model3.mj_setKeyValues(dict3)//用到mjextension
self.dataSource.append(model3)
}
self.tableView.reloadData()
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:firstCellTry = firstCellTry.init(style: UITableViewCellStyle.default, reuseIdentifier: nil)
// 創(chuàng)建cell
cell.selectionStyle = UITableViewCellSelectionStyle.none
let model:firstModelTry = self.dataSource[indexPath.row]
// 設(shè)置Model
cell.model = model
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
// 返回單元格高度
return 400 * singleLength;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
這就是全部代碼,還希望大家能提出各種寶貴意見(jiàn),以便大家互相交流,互相學(xué)習(xí)。