如何使 tableViewcell 选择在编辑时有所不同 programming_swift3?
How to make tableViewcell selection different in editing or not programming_swift3?
Image like this
我有一个想法,就是把图中红圈那两行做成。
默认单元格选择是none,切换到编辑模式选择是默认的。
如果我在编辑模式下不设置单元格选择,则不会显示复选标记。
我不想用storyboard,我是为了编程而练习的
我尝试这样做,但似乎失败了。
不知道哪里搞错了
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var rightBtn: UIBarButtonItem!
@IBOutlet weak var leftBtn: UIBarButtonItem!
var items:[String] = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","A1","B1"]
var selectedIndexs = [Int]()
override func loadView() {
super.loadView()
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
self.tableView?.allowsMultipleSelectionDuringEditing = true
rightBtn.target = self
leftBtn.target = self
rightBtn.action = #selector(btnClick(_:))
leftBtn.action = #selector(leftBtnClick(_:))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func leftBtnClick(_ sender: AnyObject) {
if(self.tableView!.isEditing == false) {
self.tableView!.setEditing(true, animated:true)
}
else {
self.tableView!.setEditing(false, animated:true)
}
}
func btnClick(_ sender: AnyObject) {
var selectedIndexs = [Int]()
if let selectedItems = tableView!.indexPathsForSelectedRows {
let sortedArray = selectedItems.sorted()
print("哈:\(sortedArray)")
for indexPath in selectedItems {
selectedIndexs.append(indexPath.row)
}
}
items.removeAt(indexes:selectedIndexs)
self.tableView?.reloadData()
self.tableView!.setEditing(false, animated:true)
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = self.items[indexPath.row]
if tableView.isEditing == true {
cell.selectionStyle = .default
}else{
cell.selectionStyle = .none
}
return cell
}
}
在 leftBtnClick 函数中添加两行。
func leftBtnClick(_ sender: AnyObject) {
if self.tableView!.isEditing == false {
self.tableView!.setEditing(true, animated:true)
self.tableView.allowsSelection = true
} else {
self.tableView!.setEditing(false, animated:true)
self.tableView.allowsSelection = false
}
}
并从 cellForRawAtIndexPath 中删除以下代码。
if tableView.isEditing == true {
cell.selectionStyle = .default
} else{
cell.selectionStyle = .none
}
Image like this
我有一个想法,就是把图中红圈那两行做成。
默认单元格选择是none,切换到编辑模式选择是默认的。
如果我在编辑模式下不设置单元格选择,则不会显示复选标记。
我不想用storyboard,我是为了编程而练习的
我尝试这样做,但似乎失败了。
不知道哪里搞错了
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var rightBtn: UIBarButtonItem!
@IBOutlet weak var leftBtn: UIBarButtonItem!
var items:[String] = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","A1","B1"]
var selectedIndexs = [Int]()
override func loadView() {
super.loadView()
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
self.tableView?.allowsMultipleSelectionDuringEditing = true
rightBtn.target = self
leftBtn.target = self
rightBtn.action = #selector(btnClick(_:))
leftBtn.action = #selector(leftBtnClick(_:))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func leftBtnClick(_ sender: AnyObject) {
if(self.tableView!.isEditing == false) {
self.tableView!.setEditing(true, animated:true)
}
else {
self.tableView!.setEditing(false, animated:true)
}
}
func btnClick(_ sender: AnyObject) {
var selectedIndexs = [Int]()
if let selectedItems = tableView!.indexPathsForSelectedRows {
let sortedArray = selectedItems.sorted()
print("哈:\(sortedArray)")
for indexPath in selectedItems {
selectedIndexs.append(indexPath.row)
}
}
items.removeAt(indexes:selectedIndexs)
self.tableView?.reloadData()
self.tableView!.setEditing(false, animated:true)
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = self.items[indexPath.row]
if tableView.isEditing == true {
cell.selectionStyle = .default
}else{
cell.selectionStyle = .none
}
return cell
}
}
在 leftBtnClick 函数中添加两行。
func leftBtnClick(_ sender: AnyObject) {
if self.tableView!.isEditing == false {
self.tableView!.setEditing(true, animated:true)
self.tableView.allowsSelection = true
} else {
self.tableView!.setEditing(false, animated:true)
self.tableView.allowsSelection = false
}
}
并从 cellForRawAtIndexPath 中删除以下代码。
if tableView.isEditing == true {
cell.selectionStyle = .default
} else{
cell.selectionStyle = .none
}