【发布时间】:2018-07-29 04:52:18
【问题描述】:
我目前正在创建一个图书馆应用程序。我有一个表格视图,并且已经设置了具有不同书名数组的单元格。我还设置了复选标记,以便用户可以选中某些标题。我刚刚在表格视图的底部添加了一个按钮,以便用户可以选中标题,然后单击该按钮以将它们从屏幕上删除,有点像检查它们。但是,由于某种原因,该按钮没有单击,并且标题也没有从屏幕上删除。我查看了该网站上的许多其他回复,我什至尝试了几个,但似乎没有一个有效。有人可以指出我正确的方向吗?我在下面发布我当前的代码。
import UIKit
class CheckOutViewController: UITableViewController {
var books = ["The Grapes of Wrath","The Great Gatsby","Anthem","Brave New World","To Kill A Mockingbird","Of Mice And Men", "Animal Farm", "War and Peace", "Don Quixote","War and Peace","Romeo and Juliet","Gulliver's Travels"]
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return books.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = books[indexPath.row]
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCellAccessoryType.checkmark{
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none
}
else{
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark
}
}
@IBAction func checkOutPressed(_ sender: Any) {
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
if editingStyle == .delete
{
books.remove(at: indexPath.row)
self.tableView.reloadData()
}
}
return true
}
}
【问题讨论】:
-
checkOutPressed中所有错误嵌套的函数是怎么回事? -
抱歉,这不是我自己的代码。这是我为这个问题找到的解决方案之一,我正在尝试,但不幸的是它没有工作。
-
发布有效代码。
-
至少 try 以了解您正在尝试的解决方案。你知道它是如何工作的吗?
-
这真的很奇怪,因为在这个表格视图中按钮没有点击,但是我的其他视图控制器中的按钮工作得很好,所以我不知道该怎么做。我还在试图弄清楚如何使用该按钮删除多行。
标签: ios swift uitableview button