iOS使用swift创建View开发

1:大家想使用混编的话,记住架桥。
2:当项目创建swift的时候,“文件名-swift”里面已经包括了Swift文件和初始化等方法,注意的一点就是当OC调用Swift的时候。直接引用头文件“文件名-swift”就可以,不要添加调用类***swift.h文件,否则文件会出现引用框架出现错误。

import UIKit

class KGCFeedBackView: UIView,UITableViewDataSource,UITableViewDelegate {

/*
 // Only override draw() if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 override func draw(_ rect: CGRect) {
 // Drawing code
 }
 */

let main = UIScreen.main.bounds.size

var mTableView:UITableView!

var testView = UIView()

let arry:[String] = ["为什么中断了学习?","难点没有讲透:","课程进度较慢,看着着急啊","不符合选课时的q期望", "太卡了,等网络好了再回来 :","不习惯在手机上学习","随便进来看看"];

//init Myinit
override init(frame:CGRect){
    super.init(frame:CGRect(x:0, y:0, width:SCREEN_HEIGHT, height:SCREEN_WIDTH));
    self.backgroundColor = UIColor(red:0.36, green:0.34, blue:0.34, alpha:0.70)
    setupSubViews()
    initTableView()
}

//View init
func setupSubViews() {
    testView = UIView(frame:CGRect(x:120, y:20, width:SCREEN_HEIGHT-240, height:SCREEN_WIDTH-40));
    testView.backgroundColor = UIColor.white;
    testView.layer.cornerRadius = 5;
    testView.layer.masksToBounds = true;
    testView.clipsToBounds = true
    testView.isHidden = false
    self.addSubview(testView)
}

//tableView init
func initTableView() {
    mTableView = UITableView(frame:CGRect(x:0, y:0, width:testView.width, height:testView.height))
    mTableView.backgroundColor = UIColor.white
    mTableView.dataSource = self
    mTableView.delegate = self
    testView.addSubview(mTableView)
    mTableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellID")
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return arry.count;
}
//#pramrk -- DataSource
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath.row == 0
    {
        return SCREEN_WIDTH/8.2+13;
    }
    return SCREEN_WIDTH/8.2;
}

//#pramrk -- Delegate
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let indentifier = String(format: "Cell%d%d",indexPath.section,indexPath.row)
    var cell:UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: indentifier)
    if cell == nil {
        cell = UITableViewCell(style: .default, reuseIdentifier: indentifier)
    }
    if indexPath.row == 0 {
        cell.backgroundColor = UIColor(red:0.45, green:0.79, blue:0.25, alpha:1.00)
    }
    cell.textLabel?.text = arry[indexPath.row]
    cell.textLabel?.font = UIFont.systemFont(ofSize: 18);
    cell.textLabel?.textAlignment = NSTextAlignment.center;
    cell.selectionStyle = UITableViewCellSelectionStyle.none
    return cell;
}
//#pramrk -- didSelectRowAt
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if indexPath.row == 0 {
        return;
    }
    let string:NSString!
    string = String(format:"%d",indexPath.row) as NSString;
    reqeustGangWeiData(Path:string)
}

//FeedBack
func reqeustGangWeiData(Path:NSString) {
    
    print(Path);
    UIApplication.shared.keyWindow?.showLoadingAnimationView()
    Common.postQuestionBankRequest(withUrl:"kgc/app/getPosition", withParm:nil, success: { (respesData:Any) in
        UIApplication.shared.keyWindow?.hidenLoadingAnimationView()
        
    }) { (error:Any) in
        UIApplication.shared.keyWindow?.hidenLoadingAnimationView()
        self.makeToast("网络异常,请稍后重试~", duration:2.0, position:CSToastPositionCenter)
    }
    self.removeFromSuperview()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

你可能感兴趣的:(iOS使用swift创建View开发)