自定义一个控制器 CustomViewController
AppDelegate(不用导入头文件很方便)
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//初始化自定义的控制器
var vc = CustomViewController()
//添加导航控制器
var nav = UINavigationController(rootViewController: vc)
//初始化Window设置为屏幕大小
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = nav
self.window?.makeKeyAndVisible()
return true
}
自定义一个UITableViewCell
import UIKit
class TestTableViewCell: UITableViewCell {
var leftImageView:UIImageView!
var label:UILabel!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
//初始化
self.leftImageView = UIImageView(frame: CGRectMake(0, 0, 100, 80))
self.leftImageView.backgroundColor = UIColor.redColor()
var leftWidth = CGRectGetMaxX(self.leftImageView.frame)
var left = CGRectGetMaxX(self.leftImageView.frame)
self.label = UILabel(frame: CGRectMake(CGRectGetMaxX(self.leftImageView.frame)+10, 0, 100, 30))
self.leftImageView.image = UIImage(named: "cell_img.jpg")
self.label.text = "text"
self.contentView.addSubview(leftImageView)
self.contentView.addSubview(label)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
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
}
}
自定义一个UITableView
import UIKit
class TestTableView: UITableView,UITableViewDataSource,UITableViewDelegate {
override init(frame: CGRect, style: UITableViewStyle) {
super.init(frame: frame, style: style)
self .initView()
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func initView() {
self.dataSource = self
self.delegate = self
self.registerClass(TestTableViewCell.self, forCellReuseIdentifier: "cellID")
}
//MARK:- UITableViewDataSource,UITableViewDelegate代理方法
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//重用cell
var cell = tableView .dequeueReusableCellWithIdentifier("cellID") as! TestTableViewCell
// cell.selectionStyle = UITableViewCellSelectionStyle.None
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//返回数量
return 10
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 100
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//cell点击效果
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
}
初始化自定义的UITableView并添加在视图上
import UIKit
//代理卸载这里
//,UITableViewDataSource,UITableViewDelegate
class CustomViewController: UIViewController {
//声明UITableView全局变量
var tableView:TestTableView!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.redColor()
//枚举的使用 UITableViewStyle.Plain 直接点出来
self.tableView = TestTableView(frame: self.view.bounds, style: UITableViewStyle.Plain)
self.view .addSubview(self.tableView)
}
}
目录:
效果图: