ios学得真是有点,首先是语言,swift和oc,其次是操作方式,xib文件,代码,storyboard,三种方式对于初学者来说非常容易增加学习成本,因为搜出来的东西很多时候不是你想要的,还要看看到底用哪种方式,还有对于不同的方式代码也不同,这也会增加成本
暂时决定以代码为主,结合xib文件,storyboard拖线的方式实在是觉得有些不适应
tableview的cell
import UIKit
class FirstCustomTableCell: UITableViewCell {
let imageView_W = 120.0//w:h = 4:3
let imageView_H = 90.0
let subView_interval:CGFloat = 10.0
var leftImageView : UIImageView?
var nameLabel : UILabel?
var subNameLabel : UILabel?
var timeLabel : UILabel?
var browseLabel : UILabel?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.selectionStyle = UITableViewCell.SelectionStyle.none
self.createCellUI()
}
func createCellUI(){
leftImageView = UIImageView.init(frame : CGRect(x:15.0,y:5.0,width:imageView_W,height:imageView_H))
leftImageView!.backgroundColor = UIColor.lightGray
self.contentView.addSubview(leftImageView!)
leftImageView?.image = UIImage(named: "timg.jpeg")
//name
nameLabel = UILabel.init(frame: CGRect(x:Max_X(object:leftImageView!) + subView_interval,y:subView_interval,width:SCREEN_WIDTH - Max_X(object:leftImageView!) - 2 * subView_interval,height:25.0))
nameLabel?.textColor = UIColor.darkText
nameLabel?.font = UIFont.systemFont(ofSize: 18)
nameLabel?.text = "世界杯开幕";
self.contentView.addSubview(nameLabel!)
subNameLabel = UILabel.init(frame: CGRect(x:X(object:nameLabel!),y:Max_Y(object: nameLabel!) + 5,width:W(object: nameLabel!),height:25))
subNameLabel?.textColor = UIColor.darkGray
subNameLabel?.font = UIFont.systemFont(ofSize: 15)
subNameLabel?.text = "世界杯开幕";
self.contentView.addSubview(subNameLabel!)
timeLabel = UILabel.init(frame: CGRect(x:X(object:nameLabel!),y:100-25,width:W(object: subNameLabel!) * 0.6,height:20))
timeLabel?.textColor = UIColor.lightGray
timeLabel?.font = UIFont.systemFont(ofSize: 13)
timeLabel?.text = "2018-01-01 10:58"
self.contentView.addSubview(timeLabel!)
browseLabel = UILabel.init(frame: CGRect(x:Max_X(object: timeLabel!),y:Y(object: timeLabel!),width:W(object: subNameLabel!) * 0.4,height:20))
browseLabel?.textAlignment = NSTextAlignment.right
browseLabel?.textColor = UIColor.lightGray
browseLabel?.font = UIFont.systemFont(ofSize: 13)
browseLabel?.text = "浏览:"+"50"
self.contentView.addSubview(browseLabel!)
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
controller
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
let cell_identifier:String = "FirstCustomTableCell"
var myTableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.navigationItem.title = "自定义TableViewCell"
self.creatViewUI()
}
func creatViewUI(){
self.myTableView = UITableView.init(frame: self.view.bounds, style: UITableView.Style.plain)
self.myTableView.tableFooterView = UIView.init()
self.myTableView.delegate = self
self.myTableView.dataSource = self
self.view .addSubview(self.myTableView)
self.myTableView.register(FirstCustomTableCell.classForCoder(), forCellReuseIdentifier: cell_identifier)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let customCell = tableView.dequeueReusableCell(withIdentifier: cell_identifier, for: indexPath)
return customCell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}