创建UITablView
import UIKit
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{
var Names = ["A","B","C","D","E","F"]
var tableView:UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView = UITableView(frame: self.view.frame, style: .plain)
self.tableView?.delegate = self
self.tableView?.dataSource = self
let headerLabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 30))
headerLabel.text = "Header"
self.tableView?.tableHeaderView = headerLabel
self.view.addSubview(tableView)
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Names.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = "reusedCell"
var cell = tableView.dequeueReusableCell(withIdentifier: identify)
if cell == nil{
cell = UITableViewCell(style: .default, reuseIdentifier: identify)
}
cell?.textLabel?.text = Names[indexPath.row]
return cell!
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 40
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.tableView?.deselectRow(at: indexPath, animated: true)
let cell = tableView.cellForRow(at: indexPath)
if cell?.accessoryType == UITableViewCell.AccessoryType.none{
cell?.accessoryType = .checkmark
print("你选择了:\(String(describing: cell?.textLabel?.text))")
}else{
cell?.accessoryType = .none
}
}
}
使用不同样式单元格
import UIKit
class CustomizeTableViewCell: UITableViewCell {
var UserImage:UIImageView!
var UserName:UILabel!
var Detail:UIButton!
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.UserImage = UIImageView(image: UIImage(named: "UserImage"))
self.UserImage.center = CGPoint(x: 30, y: 22)
self.UserName = UILabel(frame: CGRect(x: 80, y: 0, width: 120, height: 40))
self.UserName.text = "自定义单元格"
self.Detail = UIButton(frame: CGRect(x: 240, y: 8, width: 60, height: 24))
self.Detail.setTitle("详情", for: .normal)
self.Detail.backgroundColor = UIColor.gray
self.Detail.addTarget(self, action: #selector(showDetail), for: .touchUpInside)
self.addSubview(self.UserName)
self.addSubview(self.UserImage)
self.addSubview(self.Detail)
}
@objc func showDetail(){
print("显示详情信息")
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
效果图:
自定义UItableViewCell
创建一个Cocoa Touch class文件,设置父类:UITableViewCell,名称为CustomizeTableViewCell
编辑CustomizeTableViewCell:
import UIKit
class CustomizeTableViewCell: UITableViewCell {
var UserImage:UIImageView!
var UserName:UILabel!
var Detail:UIButton!
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.UserImage = UIImageView(image: UIImage(named: "UserImage"))
self.UserImage.center = CGPoint(x: 30, y: 22)
self.UserName = UILabel(frame: CGRect(x: 80, y: 0, width: 120, height: 40))
self.UserName.text = "自定义单元格"
self.Detail = UIButton(frame: CGRect(x: 240, y: 8, width: 60, height: 24))
self.Detail.setTitle("详情", for: .normal)
self.Detail.backgroundColor = UIColor.gray
self.Detail.addTarget(self, action: #selector(showDetail), for: .touchUpInside)
self.addSubview(self.UserName)
self.addSubview(self.UserImage)
self.addSubview(self.Detail)
}
@objc func showDetail(){
print("显示详情信息")
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
编辑ViewController:
import UIKit
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{
var user = ["A","B","C"]
override func viewDidLoad() {
super.viewDidLoad()
let tableView = UITableView(frame: self.view.frame)
tableView.dataSource = self
tableView.delegate = self
self.view.addSubview(tableView)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return user.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = "reusedCell"
var cell:CustomizeTableViewCell? = tableView.dequeueReusableCell(withIdentifier: identifier) as? CustomizeTableViewCell
if cell == nil{
cell = CustomizeTableViewCell(style: .default, reuseIdentifier: identifier)
}
cell?.UserName.text = user[indexPath.row]
return cell!
}
}
给文章添加章节和索引
import UIKit
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{
var Section = ["A","B","C","D","E","F","G","H","I","G","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","z"]
var Content = [["1","2","3"],["3","4"],["5","6"],["7","8"],["9","10"],["11","12"],["13","14"],["15","16"],["12","21"],["1","1"],["1","1"],["1","1"],["1","1"],["1","1"],["1","1"],["1","1"],["1","1"],["1","1"],["1","1"],["1","1"],["1","1"],["1","1"],["1","1"],["1","1"],["1","1"],["1","1"]]
var tableView:UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView = UITableView(frame: self.view.frame, style: .grouped)
self.tableView?.delegate = self
self.tableView?.dataSource = self
let headerLabel = UILabel(frame: CGRect(x: self.view.bounds.width/2, y: 0, width: self.view.bounds.width, height: 30))
headerLabel.text = "添加章节和索引"
self.tableView?.tableHeaderView = headerLabel
self.view.addSubview(tableView)
print(Content.count)
}
func numberOfSections(in tableView: UITableView) -> Int {
return Section.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Content[section].count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.Section[section]
}
func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return "有\(self.Content[section].count)个控件"
}
func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return self.Section
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = "reusedCell"
var cell = tableView.dequeueReusableCell(withIdentifier: identify)
if cell == nil{
cell = UITableViewCell(style: .default, reuseIdentifier: identify)
}
let section = indexPath.section
var data = self.Content[section]
cell?.textLabel?.text = data[indexPath.row]
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.tableView?.deselectRow(at: indexPath, animated: true)
let section = indexPath.section
var data = self.Content[section]
let alertController = UIAlertController(title: "提示", message: "你点击了:\(data[indexPath.row])", preferredStyle: .alert)
let ok = UIAlertAction(title: "确定", style: .default, handler: nil)
alertController.addAction(ok)
present(alertController, animated: true, completion: nil)
}
}
单元格的删除,插入,移动
import UIKit
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{
var SectionNum = ["delete","insert","move"]
var Content = [["A","B"],["C","D"],["E","F"]]
var tableView:UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView = UITableView(frame: self.view.frame)
tableView.dataSource = self
tableView.delegate = self
tableView.setEditing(false, animated: true)
self.view.addSubview(tableView)
let
|
请发表评论