Swift-自定义UITableViewCell和View
概要
本文主要粗略的整理两种方法用于自定义UITableViewCell和View:1、纯代码 2、XIB 。
一、纯代码自定义UITableViewCell和View
------------代码部分------------
Cell部分
import UIKit
@objc protocol UserCellDelegate {
optional func jumpToUserInfoVC()
optional func editUserName(userName:(String)->())
}
class UserTableViewCell: UITableViewCell {
var userImageView:UIImageView!;
var nameLabel:UILabel!;
var directionsLabel:UILabel!;
var delegate:UserCellDelegate?;
var zhezhi:String?;
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
}
internal func initUserCellDelegate(delegate: UserCellDelegate){
self.delegate = delegate
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.initCell()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func initCell(){
self.userImageView = UIImageView.init(frame: CGRect.init(x: 10, y: 0, width: 40, height: 40))
self.userImageView.backgroundColor = UIColor.orangeColor()
self.userImageView.layer.cornerRadius = 20
self.userImageView.layer.masksToBounds = true
contentView.addSubview(self.userImageView);
let userTap = UITapGestureRecognizer.init(target: self, action:#selector(userClick))
self.userImageView.userInteractionEnabled = true
self.userImageView.addGestureRecognizer(userTap)
self.nameLabel = UILabel.init(frame: CGRect.init(x: 60, y: 0, width: 100, height: 15))
self.nameLabel.textColor = UIColor.blackColor()
self.nameLabel.font = UIFont.systemFontOfSize(15.0)
contentView.addSubview(self.nameLabel)
self.nameLabel.text = "volientDuan"
self.directionsLabel = UILabel.init(frame: CGRect.init(x: 60, y: 18, width: 200, height: 12))
self.directionsLabel.textColor = UIColor.grayColor()
self.directionsLabel.font = UIFont.systemFontOfSize(12.0)
contentView.addSubview(directionsLabel)
self.directionsLabel.text = "Q-W-E-A-R-Q ---> 猜猜我是谁"
}
func userClick(){
self.delegate?.jumpToUserInfoVC!()
}
}
重写init(style: UITableViewCellStyle, reuseIdentifier: String?) 方法,在此方法中进行自定义操作
UIViewController--viewDidLoading()部分
override func viewDidLoad() {
super.viewDidLoad()
self.title = "我的"
self.view.backgroundColor = UIColor.lightGrayColor()
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
//初始化UITableView
// self.initTableView()
self.tableView = UITableView.init(frame: CGRect.init(x: 0, y: 0, width: self.view.bounds.size.width, height: self.view.bounds.size.height - 20),style: .Plain)
self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None
self.tableView.backgroundColor = UIColor.lightGrayColor()
self.tableView.delegate = self;
self.tableView.dataSource = self
//注册class
self.tableView.registerClass(UserTableViewCell.self, forCellReuseIdentifier: "userCell")
// Do any additional setup after loading the view, typically from a nib.
}
在cellForRowAtIndexPath方法中加载Cell
var cell = tableView.dequeueReusableCellWithIdentifier("userCell")
if cell == nil {
cell = UserTableViewCell.init(style: .Default, reuseIdentifier: "userCell")
}
if (cell as! UserTableViewCell).delegate == nil {
(cell as! UserTableViewCell).delegate = self;
}
cell?.selectionStyle = UITableViewCellSelectionStyle.None
return cell!
其它代码就不粘了O(∩_∩)O
在以上代码中需要注意(在注册后就会自动调用自定义cell中重写的初始化方法)
//在初始化UITableView后注册 class self.tableView.registerClass(UserTableViewCell.self, forCellReuseIdentifier: "userCell")
纯代码自定义View比较简单,直接上代码
class CustomView: UIView {
var imageView:UIImageView!
@IBOutlet var contentView: UIView!
override func awakeFromNib() {
super.awakeFromNib()
}
override init(frame: CGRect) {
super.init(frame: frame)
self.imageView = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: self.w, height: self.h))
self.addSubview(self.imageView)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
重写其init(frame: CGRect)方法实现自定义布局
二、利用XIB自定义UITableViewCell和View
------------XIB部分------------
cell部分
选择视图绑定Class
cellxib1.png
设置Identifier
cellxib2.png
自定义View部分
选择File's Owner 绑定Class
viewxib1.png将整个视图通过拉线重命名(代码在文章末尾)
viewxib2.png------------代码部分------------
cell部分
import UIKit
class MyEntranceTableViewCell: UITableViewCell {
@IBOutlet weak var markImageView: UIImageView!
@IBOutlet weak var markLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
self.markLabel.textColor = UIColor.grayColor()
self.markImageView.contentMode = UIViewContentMode.ScaleAspectFit
// Initialization code
}
internal func prepareCell(index:NSInteger){
var imageName:String = "icon"
var markName:String = "name"
switch index {
case 0:
imageName = "friends"
markName = "我的好友"
case 1:
imageName = "collect"
markName = "我的收藏"
case 2:
imageName = "myGod"
markName = "我的天啊"
default:
imageName = "GG"
markName = "我的blueBuff"
}
self.markLabel.text = markName
self.markImageView.image = UIImage.init(named: imageName)
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
你可以在awakeFromNib()中改变控件的相应属性...
UIViewController部分
只要把上面提到的注册class方法换成注册nib方法即可
self.tableView.registerNib(UINib(nibName: "MyEntranceTableViewCell",bundle: nil), forCellReuseIdentifier: "myEntranceCell")
自定义View代码部分(重点在初始化那里哦)
import UIKit
class CustomView: UIView {
@IBOutlet var contentView: UIView!
override func awakeFromNib() {
super.awakeFromNib()
}
override init(frame: CGRect) {
super.init(frame: frame)
NSBundle.mainBundle().loadNibNamed("CustomView", owner: self, options: nil)
self.contentView.frame = self.bounds
self.contentView.cornerRadius = self.contentView.w/2
self.addSubview(self.contentView)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
结束
小爷的处女座哦,虽然写的有点像个人笔记但还是希望能够帮助那些和我一样独自研究的Swift初学者们。写的不是很详细,但主要的应该都提到了!大家一起学习一起加油↖(ω)↗!
网友评论