本系列文章都是以有OC基礎來寫的,所以注釋不會寫太多,對基礎不夠了解的同學可能不太適合,另外本系列文章不是以教程式的形式來寫,是記錄學習過程的,所以不會有多少講解
OK,承接上一篇文章,這次開始UITableView的自定義,附帶場景使用(簡單demo)
創建工程什么的就不說了,先看一下界面然后咱們直接開始代碼:
Demo開始之前,我們想想應該會使用到屏幕寬高等一些常用的值,在OC中,咱們可以使用PCH然后宏定義,然而在Swift中,是沒有宏這個概念的,但是直接創建一個文件,在里面直接定義常量,別的文件也是可以訪問和使用的(并且不需要導入):如圖創建一個common文件
另外,在上面的界面截圖中,我們發現用到了navigationController,這個demo中我自定義了它(從上面截圖也能看出來):
class BaseNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationBar.barTintColor = UIColor.brownColor()
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
let attributes = [NSForegroundColorAttributeName : UIColor.whiteColor(),
NSFontAttributeName :UIFont.systemFontOfSize(18)];
self.navigationBar.titleTextAttributes = attributes;
}
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return .LightContent
}
}
好的,剛開始進來是一個登錄界面,所有代碼如下:
import UIKit
class ViewController: UIViewController,UITextFieldDelegate {
// let userTextF : UITextField=UITextField()
var userTextF : UITextField?
var pwdTextF : UITextField?
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = bgColor
self.createSubViews()
}
func createSubViews () {
// 背景圖標
let bgImageView = UIImageView(frame: CGRectMake((kWidth-203)/2.0, 50, 203, 200))
bgImageView.image = UIImage(named: "ICON")
self.view.addSubview(bgImageView)
// 賬號密碼
// 1.賬號
let userBgView = UIView(frame: CGRectMake(25, 250+45, kWidth-50, 45))
userBgView.backgroundColor = UIColor.whiteColor()
userBgView.layer.cornerRadius = 3
self.view.addSubview(userBgView)
let userImgView = UIImageView(frame: CGRectMake(15, 15.5, 12, 14))
userImgView.image = UIImage(named: "登陸")
userBgView.addSubview(userImgView)
self.userTextF = UITextField(frame: CGRectMake(32, 5, kWidth-50-32-10, 35))
self.userTextF!.delegate = self
self.userTextF!.text = "用戶名"
userBgView.addSubview(self.userTextF!)
// 2.密碼
let pwdBgView = UIView(frame: CGRectMake(25, 340+30, kWidth-50, 45))
pwdBgView.backgroundColor = UIColor.whiteColor()
pwdBgView.layer.cornerRadius = 3
self.view.addSubview(pwdBgView)
let pwdImgView = UIImageView(frame: CGRectMake(15, 15.5, 12, 14))
pwdImgView.image = UIImage(named: "密碼")
pwdBgView.addSubview(pwdImgView)
self.pwdTextF = UITextField(frame: CGRectMake(32, 5, kWidth-50-32-10, 35))
pwdTextF!.text = "密碼"
pwdTextF!.secureTextEntry = true
pwdBgView.addSubview(pwdTextF!)
// 登錄按鈕
let loginBtn = UIButton(type: .Custom)
loginBtn.frame = CGRectMake(25, 415+30, kWidth-50, 50)
loginBtn.backgroundColor = UIColor.brownColor()
loginBtn.layer.cornerRadius = 3
loginBtn.setTitle("登錄", forState: .Normal)
self.view.addSubview(loginBtn)
loginBtn.addTarget(self, action: "loginAction", forControlEvents: .TouchUpInside)
// 點擊別處收起鍵盤
let tapGes = UITapGestureRecognizer.init(target: self, action: "tapDo")
self.view.addGestureRecognizer(tapGes)
}
// 點擊收起鍵盤
func tapDo() {
userTextF?.resignFirstResponder()
pwdTextF?.resignFirstResponder()
}
// 點擊確定收起鍵盤
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
// 登錄按鈕
func loginAction () {
let buildListVC = BuildListViewController()
let buildListNVC = BaseNavigationController(rootViewController:buildListVC)
self.presentViewController(buildListNVC, animated: true, completion: nil)
}
}
在登錄后我們會進入一個表視圖,這個表視圖的單元格是咱們自己定義的
import UIKit
class BuildListTableViewCell: UITableViewCell {
var logoImgView : UIImageView?
var buildLabel : UILabel?
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
logoImgView = UIImageView(frame: CGRectMake(10, 15, 63, 63))
logoImgView?.image = UIImage(named: "定位.png")
self.addSubview(logoImgView!)
buildLabel = UILabel(frame: CGRectMake(101.5, 20, kWidth-130, 20))
buildLabel?.text = "西單大悅城"
buildLabel?.font = UIFont.systemFontOfSize(17)
self.addSubview(buildLabel!)
}
//這個是系統要求實現的
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
代碼中自定義的單元格加上了一個ImageView和一個Label,雖然數據是死的,但是在Swift中,另外一個類是可以直接訪問另一個類中的變量的(如果沒做任何保護的話),所以我們在配置cell的時候是可以直接賦值的,這里就不弄了,然后我們看創建表視圖那一塊:
import UIKit
class BuildListViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
var myTableView : UITableView?
var backBtn : UIButton!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = bgColor
self.title = "樓宇列表"
self.createSubViews()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.addSubview(backBtn)
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
backBtn.removeFromSuperview()
}
func createSubViews () {
// 返回按鈕
backBtn = UIButton(type: .Custom)
backBtn.frame = CGRectMake(10, 6, 44, 30)
backBtn.setTitle("返回", forState: .Normal)
backBtn.titleLabel?.font = UIFont.systemFontOfSize(16)
backBtn.titleLabel?.textColor = UIColor.whiteColor()
backBtn.addTarget(self, action: "back", forControlEvents: .TouchUpInside)
// TableView
myTableView = UITableView(frame: CGRectMake(0, 0, kWidth, kHeight), style: .Plain)
myTableView?.delegate = self
myTableView?.dataSource = self
myTableView?.tableFooterView = UIView(frame: CGRectZero)
self.view.addSubview(myTableView!)
}
func back() {
self.dismissViewControllerAnimated(true, completion: nil)
}
// datasource
//必須的
@available(iOS 2.0, *)
internal func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return 10
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 105
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print(indexPath.row)
let detailVC = DetailViewController()
self.navigationController?.pushViewController(detailVC, animated: true)
}
@available(iOS 2.0, *)
internal func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let identifier = "MyCell"
var cell = tableView.dequeueReusableCellWithIdentifier(identifier) as? BuildListTableViewCell
if cell==nil {
cell = BuildListTableViewCell.init(style: .Default, reuseIdentifier: identifier)
}
cell?.logoImgView?.image = UIImage(named: "你自己弄")
cell?.buildLabel?.text = "你自己弄"
return cell!
}
}
創建單元格的時候直接使用我們自定義的BuildListTableViewCell創建就行了
好了,這篇文章也完了,這只是一個非常簡單的demo,大家平時工作要做的肯定不止這么點的,哈哈哈。
學了這么幾天,我發現Swift還是比較容易入手的,下篇文章我會用一個小的項目,本人是做室內地圖SDK的,這次的小項目是仿我OC寫的SDK Demo,使用Swift和OC混編,集成百度地圖SDK和自己的室內SDK。在下篇文章中會講講,Swift項目已上github:https://github.com/qingmomo/Swift-die
OC版的demo在我們官網:http://www.innsmap.com 點產品左右滑找到SDK下載就行了,里面有OC版的demo,有室內需求可以聯系我們公司!