ios(5)tableview
Tableview 的相关操作
返回的行数
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return stuArray.count
}
返回tableviewcell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "StudentCell") as! StudentTableViewCell
let student = stuArray[indexPath.row]
cell.name.text = student.name
cell.age.text = student.age
cell.sex.text = student.sex
cell.type.text = "student"
cell.pic.image = UIImage(named: "1")
return cell
}
设定每个cell的高度
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 123
}
页面加载的时候自己添上数据
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
stuArray.append(Student(name: "tjw", age: "20", sex: "女"))
stuArray.append(Student(name: "xtx", age: "20", sex: "女"))
stuArray.append(Student(name: "wls", age: "19", sex: "女"))
stuArray.append(Student(name: "wl", age: "19", sex: "女"))
stuArray.append(Student(name: "wyy", age: "21", sex: "男"))
stuArray.append(Student(name: "gq", age: "22", sex: "男"))
}
设置每个按钮的功能已实现增删改查
@IBOutlet weak var nameTxt: UITextField!
@IBOutlet weak var ageTxt: UITextField!
@IBOutlet weak var sexTxt: UITextField!
@IBOutlet weak var stuTableView: UITableView!
查
@IBOutlet weak var chooseLabel: UILabel!
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
chooseLabel.text = "you choose:\(stuArray[indexPath.row].name) and she is \(stuArray[indexPath.row].sex)"
}
增
@IBAction func addClicked(_ sender: Any) {
var newname = nameTxt.text
var newage = ageTxt.text
var newsex = sexTxt.text
if case let s1 = Student(name: newname!, age: newage!, sex: newsex!) {
stuArray.append(s1)
stuTableView.reloadData()
}
nameTxt.resignFirstResponder()
ageTxt.resignFirstResponder()
sexTxt.resignFirstResponder()
nameTxt.text = ""
ageTxt.text = ""
sexTxt.text = ""
}
删
@IBAction func deleteClicked(_ sender: Any) {
stuTableView.isEditing = !stuTableView.isEditing
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete{
stuArray.remove(at: indexPath.row)
tableView.reloadData()
}
}
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let source = stuArray.remove(at: sourceIndexPath.row)
stuArray.insert(source, at: destinationIndexPath.row)
}
让图片变得可以拖动和删除
改
@IBAction func editClicked(_ sender: Any) {
if let row = stuTableView.indexPathForSelectedRow?.row{
if nameTxt.text != ""{
stuArray[row].name = nameTxt.text!
}
if ageTxt.text != ""{
stuArray[row].age = ageTxt.text!
}
if sexTxt.text != ""{
stuArray[row].sex = sexTxt.text!
}
stuTableView.reloadData()
}
}
注意
有一个identyfy和class要改变
实现效果
如何插入一段漂亮的代码片
去博客设置页面,选择一款你喜欢的代码片高亮样式,下面展示同样高亮的 代码片
.
// An highlighted block
var foo = 'bar';