前面咱们知道了 UITableView 是怎么用得, 如今咱们继续讲解和 UITableView密不可分的另外一个空间 UITableViewCell.java
UITableViewCell 显示的样式markdown
enum UITableViewCellStyle : Int {
case Default // 默认显示样式
case Value1 // 样式一
case Value2 // 样式二
case Subtitle // 副标题样式
}
UITableViewCell 选中的样式动画
enum UITableViewCellSelectionStyle : Int {
case None // 没有
case Blue // 蓝色
case Gray // 灰色
@availability(iOS, introduced=7.0)
case Default // 默认
}
UITableViewCell 编辑的样式ui
enum UITableViewCellEditingStyle : Int {
case None // 没有
case Delete // 删除
case Insert // 添加
}
UITableViewCell 辅助按钮的样式spa
enum UITableViewCellAccessoryType : Int {
case None // 没有按钮
case DisclosureIndicator // 蓝色向右的箭头
case DetailDisclosureButton // 蓝色向右的箭头以及信息按钮
case Checkmark // 复选框
@availability(iOS, introduced=7.0)
case DetailButton // 信息按钮
}
UITableViewCell 经常使用属性代理
// 1.初始化 Cell 的 Style 以及标签名
init(style: UITableViewCellStyle, reuseIdentifier: String?)
// 2.设置 Cell 的 ImageView 内容
var imageView: UIImageView? { get }
// 3.设置 Cell 的 textLabel 的内容
var textLabel: UILabel? { get }
// 4.设置 Cell 的 副标题内容
var detailTextLabel: UILabel? { get }
// 5.设置 Cell 的内容 View
var contentView: UIView { get }
// 6.设置 Cell 的背景 View
var backgroundView: UIView?
// 7.设置 Cell 被选中时的背景 View
var selectedBackgroundView: UIView!
// 8.设置 Cell 多选中得背景 View
var multipleSelectionBackgroundView: UIView?
// 9.设置 Cell 被选中时的 Style
var selectionStyle: UITableViewCellSelectionStyle
// 10.设置 Cell 编辑的 Style
var editingStyle: UITableViewCellEditingStyle { get }
// 11.设置 Cell 是否开启编辑状态
var editing: Bool
// 12.设置 Cell 的辅助按钮样式
var accessoryType: UITableViewCellAccessoryType
因为 TableViewCell 是不能够单独存在的, 因此必须得依赖于 UITableViewcode
遵照 TableView 代理协议以及数据源协议继承
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
}
自定义 TableVIewcoffeescript
func myTableView() {
var tableView = UITableView(frame: self.view.frame, style: UITableViewStyle.Plain)
tableView.dataSource = self
tableView.delegate = self
self.view.addSubview(tableView)
}
实现数据源方法图片
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
自定义 UITableViewCell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// 1.自定义 UITableViewCell 的样式以及标签, reuseIdentifier 是 Cell 得标签, 做用和 Tag 相似
var cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "cell")
// 2.设置 UITableViewCell 的标题Label
cell.textLabel!.text = "我是 Cell"
// 3.设置 UITableViewCell 的简介Label
cell.detailTextLabel?.text = "Cell"
// 4.设置 UITableViewCell 的 imageView 图片
cell.imageView?.image = UIImage(named: "image_black.jpg")
// 5.设置 UITableViewCell 的编辑模式是否开启, 以及是否执行动画效果
cell.setEditing(true, animated: true)
// 6.设置 UITableViewCell 的背景色
cell.backgroundColor = UIColor.greenColor()
// 7.设置 UITableViewCell 的编辑模式辅助按钮
cell.editingAccessoryType = UITableViewCellAccessoryType.DisclosureIndicator
// 8.设置 UITableViewCell 被选中的样式
cell.selectionStyle = UITableViewCellSelectionStyle.Default
// 9.设置 UITableViewCell 分割线的位置
cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 20)
// 10.设置 UITableViewCell 被选中时的背景View
cell.selectedBackgroundView = nil
// 11.设置 UITableViewCell 的辅助按钮样式
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
// 返回自定的 Cell
return cell
}
开启 TableViewCell 的编辑模式
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
}
PS: UITableViewCell 是继承于 UIView, 因此 UIView 里面的属性以及方法都是能够使用的.
好了, 此次咱们就讲到这里, 下次咱们继续~~