ios - 自定义移除按钮并记录移除次数

标签 ios swift

我正在尝试为社交媒体应用制作一个报告按钮。我希望有人能够单击单元格中帖子的删除按钮。然后它将为他们(但不是其他人)删除单元格并增加一个名为 reports 的变量,我可以在 Firebase 中查看该变量以确定该帖子是否不合适。我已经为以下 Post 类制作了删除功能:

import Foundation

class Post {
    var id:String
    var title: String
    var text:String
    var createdAt:Date
    var reports: Int
    func report() {
        reports+=1
    }
   init(id: String, title: String,text:String,  timestamp:Double) {
        self.id = id
        self.title = title
        self.text = text
        self.createdAt = Date(timeIntervalSince1970: timestamp / 1000)
        self.reports =  0



    }
}

这是我的删除帖子功能:

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath){



    posts.remove(at: indexPath.row)
    tableView.deleteRows(at: [indexPath], with: .fade)
}

我想要的不是说“删除”的按钮,而是说报告。我还希望每次有人报告帖子时,它都会增加报告变量。最终,我希望删除所有报告变量高于某个整数的帖子。我将如何以这种方式自定义删除功能?

最佳答案

我希望按钮不再显示“删除”,而是显示报告。 这可以通过 - yourButton.setTitle("Report", for: .normal)

轻松完成

yourButton.titleLabel?.text = "Report"

进入下一部分—— 每当有人报告时,您需要调用 API 或您使用的任何方式来更新报告计数的存储值,然后通过获取新增加的值作为响应将其反射(reflect)在您的应用中。

您可以使用Timer在您想要的时间间隔内检查报告计数器的值,如果该值超过,则删除该帖子。

编辑 - 因为没有使用按钮

您可以通过这些委托(delegate)方法更改该红色框的标题

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
    { 
       tableView.setEditing(true, animated: true) 
    }
     func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? 
    { 
     let more = UITableViewRowAction(style: .default, title: "Report") 
     { action, index in 
       print("Report button tapped")
     }
     more.backgroundColor = UIColor.blue 
     return [more] 
    }

关于ios - 自定义移除按钮并记录移除次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56522125/

相关文章:

swift - 使用 Alamofire swift 登录后如何向我的 UserProfileViewController 显示用户详细信息

ios - UITableView 的单元格从好的图像闪烁到错误的图像并在滚动后修复

objective-c - 从 Swift 将指针传递给 C 函数中的 var

ios - AVFoundation 和 UITableView

ios - 使用 NSDictionary 访问 NSJSONSerialization 数据

ios - 使用 UITableViewAutomaticDimension 时如何手动设置某些单元格的高度?

iphone - 如果应用程序关闭,如何在 iOS 中播放闹钟?

arrays - Swift 3.0 数组 "contains"参数编译器错误

出现键盘时IOS App Layout Constraint Errors

ios - 使用 Sprite 工具包制作自定义场景而不使用自动布局