【发布时间】:2020-09-13 16:09:28
【问题描述】:
我使用 mvvm 架构构建了一个应用程序。我不知道为什么func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)
它没有在我的视图控制器上被调用(基本上滑动不起作用,它可以像 1/20 一样工作)。如果我用它的方法构建表视图(基本视图)到另一个视图控制器,它工作得很好。我尝试从头开始构建我的视图控制器,但它也不影响结果,所以问题肯定出在控制器的功能上。
我的代码在哪里滑动效果很好(我这样做只是为了检查它是否在我的应用中有效:)
class tbViewController: UIViewController {
@IBOutlet weak var tableview: UITableView!
private var data = ["1","2","3","4"]
override func viewDidLoad() {
super.viewDidLoad()
tableview.delegate = self
tableview.dataSource = self
}
}
extension tbViewController: UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = self.data[indexPath.row]
return cell
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return .delete
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
tableView.beginUpdates()
self.data.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
tableView.endUpdates()
}
}
}
我的原始视图控制器滑动不起作用,我还将原始 tableview 与上面的一个交换,只是为了验证问题是否来自 tableview...我无法得到它:C。 (请检查第一个 tableview 方法):
class ExpensesViewController: UIViewController, ChartViewDelegate {
@IBOutlet private weak var thisWeek: UIButton!
@IBOutlet private weak var thisMonth: UIButton!
@IBOutlet private weak var thisYear: UIButton!
@IBOutlet private weak var totalExpensedAmount: UILabel!
@IBOutlet private weak var pieChart: PieChartView!
@IBOutlet private weak var tableView: UITableView!
private var menu: SideMenuNavigationController?
private var tableViewActions = [Action]()
private var expensesViewModel = ExpensesViewModel()
private var data = ["1","2","3","4"]
override func viewDidLoad() {
super.viewDidLoad()
initSideBar()
customizeButtons()
customizePieChart()
self.tableView.delegate = self
self.tableView.dataSource = self
}
@IBAction func didTapMenu(){
present(self.menu!, animated: true)
}
@IBAction func onClickThisWeek(_ sender: UIButton) {
setGrayBackgroundAndBlackTitleColor(for: self.thisMonth, and: self.thisYear)
sender.backgroundColor = #colorLiteral(red: 0.05509413034, green: 0.7074701786, blue: 0.4755263329, alpha: 1)
sender.setTitleColor(#colorLiteral(red: 1, green: 1, blue: 1, alpha: 1), for: UIControl.State.normal)
}
@IBAction func onClickThisMonth(_ sender: UIButton) {
setGrayBackgroundAndBlackTitleColor(for: self.thisWeek, and: self.thisYear)
sender.backgroundColor = #colorLiteral(red: 0.05509413034, green: 0.7074701786, blue: 0.4755263329, alpha: 1)
sender.setTitleColor(#colorLiteral(red: 1, green: 1, blue: 1, alpha: 1), for: UIControl.State.normal)
}
@IBAction func onClickThisYear(_ sender: UIButton) {
setGrayBackgroundAndBlackTitleColor(for: self.thisWeek, and: self.thisMonth)
sender.backgroundColor = #colorLiteral(red: 0.05509413034, green: 0.7074701786, blue: 0.4755263329, alpha: 1)
sender.setTitleColor(#colorLiteral(red: 1, green: 1, blue: 1, alpha: 1), for: UIControl.State.normal)
}
private func setGrayBackgroundAndBlackTitleColor(for button1: UIButton, and button2: UIButton){
button1.backgroundColor = #colorLiteral(red: 0.921431005, green: 0.9214526415, blue: 0.9214410186, alpha: 1)
button2.backgroundColor = #colorLiteral(red: 0.921431005, green: 0.9214526415, blue: 0.9214410186, alpha: 1)
button1.setTitleColor(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1), for: UIControl.State.normal)
button2.setTitleColor(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1), for: UIControl.State.normal)
}
private func initSideBar(){
self.menu = SideMenuNavigationController(rootViewController: MenuListController())
self.menu?.leftSide = true
self.menu?.setNavigationBarHidden(true, animated: false)
SideMenuManager.default.leftMenuNavigationController = self.menu
SideMenuManager.default.addPanGestureToPresent(toView: self.view)
}
private func customizeButtons(){
self.thisWeek.setTitleColor(#colorLiteral(red: 1, green: 1, blue: 1, alpha: 1), for: UIControl.State.normal)
self.thisWeek.backgroundColor = #colorLiteral(red: 0.05509413034, green: 0.7074701786, blue: 0.4755263329, alpha: 1)
self.thisWeek.layer.borderColor = #colorLiteral(red: 0.05509413034, green: 0.7074701786, blue: 0.4755263329, alpha: 1)
self.thisMonth.layer.borderColor = #colorLiteral(red: 0.05509413034, green: 0.7074701786, blue: 0.4755263329, alpha: 1)
self.thisYear.layer.borderColor = #colorLiteral(red: 0.05509413034, green: 0.7074701786, blue: 0.4755263329, alpha: 1)
}
private func customizePieChart(){
self.pieChart.delegate = self
self.pieChart.noDataText = "You don't have any income, to display quarters income."
self.pieChart.legend.enabled = false
let pieChartAttribute = [ NSAttributedString.Key.font: UIFont(name: "Arial", size: 16.0)!, NSAttributedString.Key.foregroundColor: UIColor.init(displayP3Red: 0.462, green: 0.838, blue: 1.000, alpha: 1) ]
let pieChartAttrString = NSAttributedString(string: "Quarterly Revenue", attributes: pieChartAttribute)
self.pieChart.centerAttributedText = pieChartAttrString
}
}
extension ExpensesViewController: UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = self.data[indexPath.row]
return cell
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return .delete
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
tableView.beginUpdates()
self.data.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
tableView.endUpdates()
}
}
}
我的屏幕现在怎么样了,我基本上推荐了所有功能,不要让你感到困惑,我的所有业务逻辑都在哪里,这就是为什么什么都没有显示,只是一些 UI 改变,视图模型不与控制器交互(如你所见在我上面的代码中):
如果您遇到此类错误,请帮助我。非常感谢。
【问题讨论】:
标签: ios swift xcode uitableview swipe