// 实例化影讯导航控制器
letmoviewVC =MovieViewController()
// 给控制器添加导航标题
moviewVC.navigationItem.title="影讯"
// 实例化导航
letmoviewNav =UINavigationController.init(rootViewController:moviewVC)
moviewNav.tabBarItem=UITabBarItem(tabBarSystemItem:UITabBarSystemItem.more, tag:100)
// 标签栏控制器
lettabCtl =UITabBarController()
tabCtl.viewControllers= [moviewNav,loginNav,newsNav]
self.window?.rootViewController= tabCtl
===================表格====================
class MovieViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
// 表格属性
vartable:UITableView=UITableView(frame:CGRect(x:0, y:0, width:scrWidth, height:scrHeight), style:UITableViewStyle.grouped)
overridefuncviewDidLoad() {
super.viewDidLoad()
// 设置表格的数据源或代理
self.table.dataSource=self
self.table.delegate=self
self.view.addSubview(self.table)
// Do any additional setup after loading the view.
}
overridefuncdidReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - >>>>>>>UITbaleViewDatasource<<<<<<<<<<
//========================= ============================
funcnumberOfSections(in tableView:UITableView) ->Int{
returntableData.count
}
// 根据分区下标返回每个分区中有多少行
functableView(_tableView:UITableView, numberOfRowsInSection section:Int) ->Int{
// 得到表格字典的key的数组
letkeyArr =self.tableData.keys
// 通过分区下标得到该分区对应的key
letkey = keyArr[keyArr.index(keyArr.startIndex,offsetBy:section)]
// 通过key得到对应的value,这个value是个数组
letsectionArr =self.tableData[key]
return(sectionArr?.count)!
}
functableView(_tableView:UITableView, cellForRowAt indexPath:IndexPath) ->UITableViewCell{
letidentifier ="cell"
varcell = tableView.dequeueReusableCell(withIdentifier: identifier)
ifcell ==nil{
cell =UITableViewCell(style: .subtitle, reuseIdentifier: identifier)
}
// 得到所有的key的数组
letkeyArr =self.tableData.keys
letkey = keyArr[keyArr.index(keyArr.startIndex,offsetBy:indexPath.section)]
// 根据key得到分区的数组
letsectionArr =self.tableData[key]
// 根据行的下标得到改行对应的字典
letrowDic = sectionArr![indexPath.row]
cell?.textLabel?.text= rowDic["name"]
cell?.detailTextLabel?.text= rowDic["author"]
returncell!
}
// 设置分区标题
functableView(_tableView:UITableView, titleForHeaderInSection section:Int) ->String? {
// 获取所有key的数组
letkeyArr =self.tableData.keys
// 根据分区下标获取对应的key
letkey = keyArr[keyArr.index(keyArr.startIndex,offsetBy: section
)]
returnkey
}
======================登录========================
protocolloginViewDelegate:class {
// 执行登录所做的回调
funcloginHandle(account:String) ->Void
}
class LoginView: UIView ,UITextFieldDelegate{
privatevarbackImageView:UIImageView?
privatevaraccountTF:UITextField?
privatevarloginBtn:UIButton?
// 定义代理属性
weak var delegate:loginViewDelegate?
overrideinit(frame:CGRect) {
super.init(frame: frame)
// 背景视图
self.backImageView=UIImageView(frame:CGRect(x:0, y:0, width:scrWidth, height:scrHeight))
self.backgroundColor = UIColor.yellow
self.accountTF=UITextField(frame:CGRect(x:20, y:150, width:scrWidth-40, height:60))
self.accountTF?.placeholder="请输入账号"
self.accountTF?.delegate=self
self.accountTF?.textAlignment= .center
self.accountTF?.borderStyle= .line
self.addSubview(self.accountTF!)
self.loginBtn=UIButton(frame:CGRect(x:45, y:230, width:scrWidth-90, height:60))
self.loginBtn?.setTitle("登录", for: .normal)
self.loginBtn?.backgroundColor = UIColor.red
self.loginBtn?.addTarget(self, action:#selector(loginBtnDidPress(sender:)), for: .touchUpInside)
self.addSubview(self.loginBtn!)
}
@objcfuncloginBtnDidPress(sender:UIButton) ->Void{
ifletacc =self.accountTF?.text{
if!acc.isEmpty{
self.delegate?.loginHandle(account: acc)
}else{
print("数据不可为空")
}
}
else
{
print("数据不可为空")
}
}
requiredinit?(coder aDecoder:NSCoder) {
fatalError("init(coder:) has not been implemented")
}
functextFieldShouldReturn(_textField:UITextField) ->Bool{
textField.resignFirstResponder()
return true
}
overridefunctouchesBegan(_touches:Set, with event:UIEvent?) {
self.endEditing(true)
}
}