2016-12-21 17_15_35.gif
//
// ViewController.swift
// tableview的创建
//
// Created by mibo02 on 16/12/21.
// Copyright © 2016年 mibo02. All rights reserved.
//
import UIKit
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{
var tableView: UITableView!
var dataArray = NSMutableArray()
var imageArray = NSMutableArray()
let cellID = "testCellID"
var isEdit = false//判断tableview是否是在编辑状态
override func viewDidLoad() {
super.viewDidLoad()
self.title = "UITableView"
//
self.imageArray = ["1.jpg","1.jpg","1.jpg","1.jpg","1.jpg","1.jpg","1.jpg","1.jpg"]
self.dataArray = ["first","second","three","four","five","six","seven","eight"]
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "编辑", style: UIBarButtonItemStyle.done, target: self, action: #selector(ViewController.rightBarButtonItemClicked))
//初始化TableView
self.tableView = UITableView(frame: CGRect(x:0,y:0, width:self.view.frame.size.width,height:self.view.frame.size.height), style: UITableViewStyle.plain)
self.tableView.dataSource = self
self.tableView.delegate = self
self.tableView.rowHeight = 50
self.tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: cellID)
self.view.addSubview(self.tableView)
self.tableView.tableFooterView = UIView()
}
func rightBarButtonItemClicked() {
if isEdit {
self.tableView.setEditing(false, animated: true)
isEdit = false
} else {
self.tableView.setEditing(true, animated: true)
isEdit = true
}
}
//返回cell的行数
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.dataArray.count
}
//返回cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath)
cell.textLabel?.text = self.dataArray[indexPath.row] as? String
cell.imageView?.image = UIImage(named: self.imageArray[indexPath.row] as! String)
return cell;
}
//可被编辑
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
//确定编辑模式(默认是滑动删除模式)
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.delete
}
//具体编辑操作(默认删除操作)
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
//
self.dataArray.removeObject(at: indexPath.row)
//
self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.top)
}
//允许移动某一行
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
//实现排序
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
//后面加as。。。
let object:AnyObject = self.dataArray[sourceIndexPath.row] as AnyObject
//
self.dataArray.removeObject(at: sourceIndexPath.row)
self.dataArray.insert(object, at: destinationIndexPath.row)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
网友评论