swift-UITableView

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

自定义一个控制器 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)

}

}

目录:

效果图:

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航