gpt4 book ai didi

uitableview - 确认删除 TableView 单元格中的帖子?

转载 作者:行者123 更新时间:2023-11-30 10:14:54 25 4
gpt4 key购买 nike

我有一个UItableview,其中有显示用户帖子的单元格。

我希望用户能够使用帖子中显示的“删除按钮”来删除他们的帖子。

我可以做到这一点,但我希望用户在单击单元格中的删除按钮时首先弹出确认窗口。

因此,我将下面的代码添加为表的“单元格”文件中的操作,但出现错误,提示“使用未解析的标识符presentviewcontroller”。

我可以不在单元文件中使用presentviewcontroller吗?

@IBAction func button_clicked(sender: AnyObject) {
var refreshAlert = UIAlertController(title: "Refresh", message: "Do you want to delete this post?", preferredStyle: UIAlertControllerStyle.Alert)

refreshAlert.addAction(UIAlertAction(title: "Yes", style: .Default, handler: { (action: UIAlertAction!) in
println("Handle Ok logic here")
}))

refreshAlert.addAction(UIAlertAction(title: "No", style: .Default, handler: { (action: UIAlertAction!) in
println("Handle Cancel Logic here")
}))

presentViewController(refreshAlert, self, completion: nil)
}

最佳答案

嗯,最好在 View Controller 中使用警报控件,因为在 Controller 中,您可以获取诸如 TableView 之类的所有内容(例如删除注释后您必须重新加载),要删除的数据(存在于(对于示例)用于在 TableView 中显示的数组)...等

首先在单元文件中定义一个委托(delegate)方法,例如

 import UIKit
@objc protocol CellDelegate
{
func deleteCell(cell:CustomCommentCell)
}
class CustomCommentCell: UITableViewCell {
@IBOutlet weak var deleteButton: UIButton! //a delete button
weak var delegate:CellDelegate? //declare a delegate

override init(style: UITableViewCellStyle, reuseIdentifier: String?)
{
super.init(style: style, reuseIdentifier: reuseIdentifier)
}

required init(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)
}

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}

//other code
//......
@IBAction func button_clicked(sender: AnyObject)
{
self.delegate?.deleteCell(self) //call the delegat method
}

ViewController

import UIKit
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate, CellDelegate,UIAlertViewDelegate // add `CellDelegate`,UIAlertViewDelegate if u want t use alert view
{
//...other code
// ....

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell:CustomCommentCell? = tableView.dequeueReusableCellWithIdentifier("CELL") as? CustomCommentCell;
if(cell == nil)
{
//..cell initilise
}
cell?.delegate = self //set the delegate to self
//..other code set up comment string .. etc
return cell!;
}

//define custom delegate method hear u can delete the cell
//since u are passing the cell so u can get the index path of the cell
func deleteCell(cell: CustomCommentCell)
{
var deleteCell:CustomCommentCell? = cell as CustomCommentCell
var indexPath: NSIndexPath = self.tableView.indexPathForCell(deleteCell!)! //get the index path
//using alert view
var alertToDelete: UIAlertView = UIAlertView(title: "Delete", message: "Are u sure to delete this comment", delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "Ok")
alertToDelete.show()

/* uncomment if u want to use alertControl and comment above 2 line of alertView
//using alert control
var refreshAlert = UIAlertController(title: "Refresh", message: "Do you want to delete this post?", preferredStyle: UIAlertControllerStyle.Alert)
refreshAlert.addAction(UIAlertAction(title: "Yes", style: .Default, handler: { (action: UIAlertAction!) in
println("Handle Ok logic here")
//after deleting from datasource
self.tableView.reloadData()
}))

refreshAlert.addAction(UIAlertAction(title: "No", style: .Default, handler: { (action: UIAlertAction!) in
println("Handle Cancel Logic here")
}))

self.presentViewController(refreshAlert, animated: true, completion: nil)
*/

}

//suppose if u use alert view u will get delegate call back in this check which button is clicked
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
if(buttonIndex == alertView.cancelButtonIndex)
{
//do nothing
println("Handle Cancel Logic here")
}
else
{
//delete hear
println("Handle Ok logic here")
//after deleting form datasource
self.tableView.reloadData()
}
}

关于uitableview - 确认删除 TableView 单元格中的帖子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30726813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com