我不明白为什么滑动不起作用?我想我写的一切都正确吗?这是我的代码:
class ViewController: UIViewController, UITableViewDataSource, GetData, UISearchBarDelegate, UITableViewDelegate{
//Soccuper des date pour les avoirs en milliseconde
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var viewConstraint: NSLayoutConstraint!
@IBOutlet weak var blurView: UIVisualEffectView!
@IBOutlet weak var sideView: UIView!
@IBOutlet weak var usersTableView: UITableView!
//Variable
public static let db: UserDatabase = UserDatabase();
var users : Array<User> = Array();
var filteredData: Array<User> = Array();
var isSearching: Bool = false;
override func viewDidLoad() {
super.viewDidLoad()
//--------Navigation Side menu--------------
sideView.layer.shadowColor = UIColor.black.cgColor;
sideView.layer.shadowOpacity = 0.3; //Opacité des ombres 0.8 de base
sideView.layer.shadowOffset = CGSize(width: 3, height: 0);
viewConstraint.constant = -290;
//-------------------------------------
//------------Searchbar----------------
searchBar.barTintColor = .white;
//-------------------------------------
//----------TableView---------------
usersTableView.dataSource = self; //Initialize table view
usersTableView.delegate = self; //Pour le on click
usersTableView.separatorStyle = .none
//----------------------------------
refreshList(); //On rafraichis la listView en mettant a jour les arraylist
//Pour le seachbar on initialize ci dessous
searchBar.delegate = self;
}
//----------------------Table View-----------------------
func goToEditFromAlertView(user: User) {
performSegue(withIdentifier: "addNewUser", sender: user)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return users.count;
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let deleteAction = UITableViewRowAction(style: .normal, title: "Delete") { (rowAction, indexPath) in
//TODO: Delete the row at indexPath here
}
deleteAction.backgroundColor = .red
return [deleteAction]
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "userCell", for: indexPath) as? UserTableViewCell;
cell?.delegate = self; //Pour lui permettre de recevoir un delegate
cell?.view(with: users[indexPath.row]);
cell?.myVC = self;
//Pour changer la couleur de la cellule selectionné
let bgColorView = UIView()
bgColorView.backgroundColor = UIColor.white
cell?.selectedBackgroundView = bgColorView
return cell!;
}
func refreshList(){
users.removeAll();
users = ViewController.db.getData()
filteredData = users
usersTableView.reloadData()
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String){
let searchText = searchBar.text?.lowercased()
users.removeAll();
if(searchText == nil || searchText == "" || searchText?.count == 0){
users = filteredData;
}else{
for u: User in filteredData{
if((u.getName().lowercased().range(of: searchText!)) != nil){
users.append(u);
usersTableView.reloadData();
}
}
}
usersTableView.reloadData();
}
//-------------------------------------------------------
我使用customtableview单元格。
答案 0 :(得分:1)
您需要添加此UITableView
委托方法:
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
顺便说一下,分号(;
)在swift中是不必要的。
答案 1 :(得分:1)
对于Swift 3&amp;斯威夫特4:
您需要实现两个不同的委托。第一个是
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
还要添加UITableViewDataSource tableView(:commit:forRowAt :)方法:
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
// Action here
// In case of delete, you can simply do:
if editingStyle == .delete {
//Remove item at relative position from datasource array
//Reload tableview at the respective indexpath
}
}