UIKit框架-高级控件Swift版本: 3.UITableViewCell方法/属性详解

前面我们知道了 UITableView 是怎么用得, 现在我们继续讲解和 UITableView密不可分的另一个空间 UITableViewCell.


1.UITableViewCell常用属性

UITableViewCell 显示的样式

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 编辑的样式

enum UITableViewCellEditingStyle : Int {   
    case None // 没有
    case Delete // 删除
    case Insert // 添加
}

UITableViewCell 辅助按钮的样式

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

2.代码演示

由于 TableViewCell 是不可以单独存在的, 所以必须得依赖于 UITableView

遵守 TableView 代理协议以及数据源协议

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
}

自定义 TableVIew

    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) {
    }

3.最终效果

1


PS: UITableViewCell 是继承于 UIView, 所以 UIView 里面的属性以及方法都是可以使用的.

好了, 这次我们就讲到这里, 下次我们继续~~

原文地址:https://www.cnblogs.com/iOSCain/p/4529346.html