写一个简单的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());
}
}