前面我们知道了 UITableView 是怎么用得,现在我们继续讲解和 UITableView密不可分的另一个空间 UITableViewCell.
1.UITableViewCell常用属性
UITableViewCell 显示的样式
@H_404_9@enum UITableViewCellStyle : Int { case Default // 默认显示样式 case Value1 // 样式一 case Value2 // 样式二 case Subtitle // 副标题样式 }UITableViewCell 选中的样式
@H_404_9@enum UITableViewCellSelectionStyle : Int { case None // 没有 case Blue // 蓝色 case Gray // 灰色 @availability(iOS,introduced=7.0) case Default // 默认 }UITableViewCell 编辑的样式
@H_404_9@enum UITableViewCellEditingStyle : Int { case None // 没有 case Delete // 删除 case Insert // 添加 }UITableViewCell 辅助按钮的样式
@H_404_9@enum UITableViewCellAccessoryType : Int { case None // 没有按钮 case disclosureIndicator // 蓝色向右的箭头 case DetaildisclosureButton // 蓝色向右的箭头以及信息按钮 case checkmark // 复选框 @availability(iOS,introduced=7.0) case DetailButton // 信息按钮 }UITableViewCell 常用属性
@H_404_9@// 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: UITableViewCellAccessoryType2.代码演示
由于 TableViewCell 是不可以单独存在的,所以必须得依赖于 UITableView
遵守 TableView 代理协议以及数据源协议
@H_404_9@class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate { }自定义 TableVIew
@H_404_9@func myTableView() { var tableView = UITableView(frame: self.view.frame,style: UITableViewStyle.Plain) tableView.dataSource = self tableView.delegate = self self.view.addSubview(tableView) }实现数据源方法
@H_404_9@func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int { return 5 }自定义 UITableViewCell
@H_404_9@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,20) // 10.设置 UITableViewCell 被选中时的背景View cell.selectedBackgroundView = nil // 11.设置 UITableViewCell 的辅助按钮样式 cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator // 返回自定的 Cell return cell }开启 TableViewCell 的编辑模式
@H_404_9@func tableView(tableView: UITableView,commitEditingStyle editingStyle: UITableViewCellEditingStyle,forRowAtIndexPath indexPath: NSIndexPath) { }3.最终效果
PS: UITableViewCell 是继承于 UIView,所以 UIView 里面的属性以及方法都是可以使用的.
好了,这次我们就讲到这里,下次我们继续~~
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。