我正在制作一个表格 View ,我想制作一个功能,您可以通过向右滑动并点击删除按钮来删除一行。我和我的老师已经尝试了大约半小时来解决这个问题,但似乎没有任何效果。
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var StoredValues = Values()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {//
super.didReceiveMemoryWarning()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return UITableViewCell()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "meunSegue", sender: self)
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
_ = segue.destination as! SecondViewController
}
}
func tableView->(UITableView *)tableView canEditRowsAtIndexPath:(NSIndexPath *)indexPath {
return YES
}
- (void)tableView:(UITableView *)tableView committEditStyle: (UITableViewCellEditingStyle)
func tableView {
var cell = UITableView.self
var Animation = UITableViewRowAnimation(rawValue: 1)
if editingStyle == UITableViewCellEditingStyle.delete {
cell.deleteRows(indexPath)
//cell.deleteRows(at: [NSIndexPath], with: UITableViewRowAnimation.automatic)
}
}
class SecondViewController: UIViewController {
var recievedData = ""
override func viewDidLoad() {
super.viewDidLoad()
print(recievedData)
}
}
}
最佳答案
这是因为您的 numberOfRowsInSection
数据源实现始终返回 1(固定)。
通常,您将对象存储在一个数组中,该数组定义了行数。并且,commitEditingStyle
应该从数组中删除对象,然后删除行。
– (void)tableView: (UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath: (NSIndexPath *)indexPath {if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[maTheData removeObjectAtIndex:[indexPath row]];
// Delete row using the cool literal version of [NSArray arrayWithObject:indexPath]
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
关注这个link了解更多详情。
关于swift - UITableView 删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43744911/