iOS开发——Swift快速上手(开发篇)


  写一个简单的tableview和自定义cell做个实际项目基本开发纯演示代码。

import Foundation
import HandyJSON

let screenWith   = UIScreen.main.bounds.size.width
let screenHeight = UIScreen.main.bounds.size.height

let wordSize : CGFloat = 14.0

let titleCellID    : String = "titleCellID"
let textInfoCellID : String = "textInfoCellID"
let affirmCellID   : String = "affirmCellID"



class CustomCell : UITableViewCell{
    
    var dataAdapter : DataAdapter?
    var indexPath   : IndexPath?
    var display     : Bool?
    
    func loadData() {
        
    }
}

//MARK:标题cell
class titleCell: CustomCell {
    
    
    fileprivate var textInfo : UILabel?
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.selectionStyle    = UITableViewCellSelectionStyle.none;
        self.backgroundColor   = colorWithHexString(hex: "8BC9D7")
        
        textInfo = UILabel.init(frame: CGRect(x:0,y:0,width:screenWith,height:40));
        textInfo?.font          = UIFont.boldSystemFont(ofSize:wordSize+2)
        textInfo?.textAlignment = NSTextAlignment.center
        textInfo?.textColor     = UIColor.white
        
        
        
        self.addSubview(textInfo!);
        
        
    }
    
    
    override func loadData()  {
        
        let textString =  dataAdapter?.data as! String
        
        textInfo?.text = textString;
        
        
        
    }
    
    
    
}


//MARK:适当性cell
class textInfoCell: CustomCell {
    
    fileprivate var textInfo : UILabel?
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.selectionStyle    = UITableViewCellSelectionStyle.none;
        self.backgroundColor   = UIColor.white;
        
        textInfo = UILabel.init(frame: CGRect(x:15,y:0,width:screenWith-30,height:self.frame.size.height));
        textInfo?.textAlignment = NSTextAlignment.left
        textInfo?.textColor     = UIColor.white
        textInfo?.numberOfLines = 0
        
        self.addSubview(textInfo!);
        
        
    }
    
    override func loadData() {
        
        
        let H :CGFloat =  (dataAdapter?.cellHeight)!
        textInfo?.frame = CGRect(x:15,y:0,width:screenWith-30,height:H-15)
        textInfo?.attributedText = dataAdapter?.data as? NSAttributedString
        
        
    }
    
    
    
}
class AppropriatenessViewController: BaseOrderTypeViewController,UITableViewDataSource,UITableViewDelegate{

   
   
                        var riskTipView: UIView!               var table : UITableView!     var dataArray: NSMutableArray!          var highestRiskVC:HighestRiskViewController? = nil               override func viewDidLoad() {         super.viewDidLoad()
 self.dataArray = NSMutableArray()
self.table = UITableView.init(frame: CGRect(x:0,y:83,width:screenWith,height:(self.view.frame.size.height-83*2+20)));         self.table.separatorStyle = UITableViewCellSeparatorStyle.none;                           self.table.dataSource = self;         self.table.delegate   = self;                  self.table.register(titleCell.self, forCellReuseIdentifier: titleCellID)         self.table.register(textInfoCell.self, forCellReuseIdentifier: textInfoCellID)         self.table.register(affirmBtnCell.self, forCellReuseIdentifier: affirmCellID)                  self.view.addSubview(self.table)                           // 未风险评测提示         self.riskTipView = UIView.init(frame: CGRect(x:0,y:83,width:screenWith,height:(self.view.frame.size.height-83*2+20)))         self.riskTipView.backgroundColor = UIColor.white         self.riskTipView.isHidden        = true         self.view.addSubview(self.riskTipView)                  let infoLabel = UILabel.init(frame: CGRect(x:15, y:30, width:screenWith-30, height: 80))         infoLabel.backgroundColor = UIColor.white         infoLabel.numberOfLines   = 0         infoLabel.textAlignment   = .left         infoLabel.text            = "您未做过风险承受能力测评或测评结果已过期,请点此进行测评"         infoLabel.font            =  UIFont.boldSystemFont(ofSize: wordSize)         riskTipView.addSubview(infoLabel)                  let goRiskButton          = UIButton.init(type: .custom)         goRiskButton.frame        = CGRect(x:(screenWith/2-60), y:130, width:120, height: 40)         goRiskButton.backgroundColor =  colorWithHexString(hex: "4372A1")         goRiskButton.setTitle("填写问卷", for: .normal)         goRiskButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)         goRiskButton.setTitleColor(UIColor.white, for: .normal)         goRiskButton.addTarget(self, action: #selector(gotoWenjuan), for: .touchUpInside)         self.riskTipView.addSubview(goRiskButton)                           let infobuttom = UILabel.init(frame: CGRect(x:15, y:180, width:screenWith-30, height: 80))         infobuttom.backgroundColor = UIColor.white         infobuttom.numberOfLines   = 0         infobuttom.textAlignment   = .center         infobuttom.text            = "完成评测后,请刷新本页,您将可以继续购买"         infobuttom.font            =  UIFont.systemFont(ofSize: wordSize)         riskTipView.addSubview(infobuttom)
}
func dealData() {                  self.dataArray.removeAllObjects()                                    let data0 : DataAdapter = DataAdapter()         data0.cellReuseIdentifier = titleCellID         if let errorMessage = self.currentOrderCombined?.errorMessage {             if errorMessage == "SuitabilityDismatch" {                                   data0.data                = "金融产品不适当警示" as AnyObject             }else{                 data0.data                = "适当性评估结果确认书" as AnyObject             }         }         data0.cellHeight          = 40;                           let data1 : DataAdapter = DataAdapter()         data1.cellReuseIdentifier = textInfoCellID         let richText1:NSAttributedString  = getAttributedStrM(type: 1)         data1.data                = richText1         data1.cellHeight          = MathUtil.getRichTextHeight(textStr: richText1, width: screenWith-30)+20                           let data2 : DataAdapter = DataAdapter()         data2.cellReuseIdentifier = titleCellID         data2.data                = "客户投资确认书" as AnyObject         data2.cellHeight          = 40                  let data3 : DataAdapter = DataAdapter()         data3.cellReuseIdentifier = textInfoCellID         let richText2:NSAttributedString  = getAttributedStrM(type: 3)         data3.data                = richText2         data3.cellHeight          =  MathUtil.getRichTextHeight(textStr: richText2, width: screenWith-30)+20                           let data4 : DataAdapter   = DataAdapter()         data4.cellReuseIdentifier = affirmCellID         data4.cellHeight          = 60                           self.dataArray.add(data0)         self.dataArray.add(data1)         self.dataArray.add(data2)         self.dataArray.add(data3)         self.dataArray.add(data4)                           self.table.reloadData()     }               //MARK:tabeleView-代理     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) ->Int{                  return self.dataArray.count     }          func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {                  let dataAdapter : DataAdapter = self.dataArray[indexPath.row] as! DataAdapter;                  return dataAdapter.cellHeight              }               func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {                  let adapter : DataAdapter = self.dataArray[indexPath.row] as!DataAdapter;                  let cell :CustomCell = tableView.dequeueReusableCell(withIdentifier: adapter.cellReuseIdentifier) as! CustomCell                  cell.dataAdapter = adapter;         cell.indexPath   = indexPath;         cell.loadData()                  return cell     }          func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {                  if indexPath.row == 4 {                          self.buyAction(UIButton.init());         }     }
 
 
 
 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值