UITableView 中带有布尔值的切换按钮 Swift
toggle button with boolean in UITableView Swift
我想添加一个布尔切换以了解按钮是否已被按下(会将此值保存到核心数据(如果为真,还想将单元格数据保存到核心数据,如果为假,则从核心数据中删除) ) 我不知道该怎么做。任何帮助将不胜感激。如果需要来自视图控制器的所有代码,请发表评论,我会这样做(我已经设置了 xcdatamodel)。
@IBAction func buttonTapped(_ sender:UIButton) {
var superView = sender.superview
while !(superView is UITableViewCell) {
superView = superView?.superview
}
let cell = superView as! UITableViewCell
if let indexpath = tableView.indexPath(for: cell){
print(indexpath)
}
}
将 属性 添加到您的自定义单元格,例如:
var indexPath: IndexPath = IndexPath(row: -1, section: -1) //The -1 just know when it is not set yet
并且在 cellForRow
的 tableView 中:
cell.indexpath = indexPath
假设 buttonTapped
在您的自定义单元格中定义 class:
@IBAction func buttonTapped(_ sender:UIButton) {
print(indexpath)
}
向您的单元格添加委托方法- (void)myCell:(MyCell *)cell didTapButton:(UIButton *)button
那么在您的 VC 中,您可以从单元格对象向后工作。
- (void)myCell:(MyCell *)cell didTapButton:(UIButton *)button
{
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
MyObject *object = [self.dataSource objectAtIndexPath:indexPath];
// Do something with the knowledge that the button in the cell for the object was tapped...
// For example
object.tapped = YES;
[object.managedObjectContext save:NULL];
}
只需要转换成swift ;)
好吧,我构建了一个简单的项目,并使用协议想出了一些办法,
首先你定义一个这样的协议:
protocol cellDidRequestSaving {
func saveOrDelete(indexpath : Int)
}
首先在您的单元格中定义按钮,如下所示:
class TableViewCell: UITableViewCell {
var delegate: cellDidRequestSaving?
var indexPath = 0 //come from the parent
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
@IBAction func didTap(_ sender: Any) {
// this protocol defined in the parent
delegate?.saveOrDelete(indexpath: indexPath)
}
}
现在你 tableViewController
你使用这样的协议:
class TableViewController: UITableViewController, cellDidRequestSaving {
var cellStat = [Int:Bool]()
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 3
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
cell.indexPath = indexPath.row
cell.delegate = self
// Configure the cell...
return cell
}
func saveOrDelete(indexpath: Int) {
if let status = cellStat[indexpath], status {
print(indexpath, "delete")
cellStat[indexpath] = !status
}
else {
print(indexpath, "save")
cellStat[indexpath] = true
}
}
这是一个简单的项目,但您可以了解如何做。另外,请注意协议定义和用法,这样您就不会错过任何东西。
输出结果是
我想添加一个布尔切换以了解按钮是否已被按下(会将此值保存到核心数据(如果为真,还想将单元格数据保存到核心数据,如果为假,则从核心数据中删除) ) 我不知道该怎么做。任何帮助将不胜感激。如果需要来自视图控制器的所有代码,请发表评论,我会这样做(我已经设置了 xcdatamodel)。
@IBAction func buttonTapped(_ sender:UIButton) {
var superView = sender.superview
while !(superView is UITableViewCell) {
superView = superView?.superview
}
let cell = superView as! UITableViewCell
if let indexpath = tableView.indexPath(for: cell){
print(indexpath)
}
}
将 属性 添加到您的自定义单元格,例如:
var indexPath: IndexPath = IndexPath(row: -1, section: -1) //The -1 just know when it is not set yet
并且在 cellForRow
的 tableView 中:
cell.indexpath = indexPath
假设 buttonTapped
在您的自定义单元格中定义 class:
@IBAction func buttonTapped(_ sender:UIButton) {
print(indexpath)
}
向您的单元格添加委托方法- (void)myCell:(MyCell *)cell didTapButton:(UIButton *)button
那么在您的 VC 中,您可以从单元格对象向后工作。
- (void)myCell:(MyCell *)cell didTapButton:(UIButton *)button
{
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
MyObject *object = [self.dataSource objectAtIndexPath:indexPath];
// Do something with the knowledge that the button in the cell for the object was tapped...
// For example
object.tapped = YES;
[object.managedObjectContext save:NULL];
}
只需要转换成swift ;)
好吧,我构建了一个简单的项目,并使用协议想出了一些办法, 首先你定义一个这样的协议:
protocol cellDidRequestSaving {
func saveOrDelete(indexpath : Int)
}
首先在您的单元格中定义按钮,如下所示:
class TableViewCell: UITableViewCell {
var delegate: cellDidRequestSaving?
var indexPath = 0 //come from the parent
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
@IBAction func didTap(_ sender: Any) {
// this protocol defined in the parent
delegate?.saveOrDelete(indexpath: indexPath)
}
}
现在你 tableViewController
你使用这样的协议:
class TableViewController: UITableViewController, cellDidRequestSaving {
var cellStat = [Int:Bool]()
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 3
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
cell.indexPath = indexPath.row
cell.delegate = self
// Configure the cell...
return cell
}
func saveOrDelete(indexpath: Int) {
if let status = cellStat[indexpath], status {
print(indexpath, "delete")
cellStat[indexpath] = !status
}
else {
print(indexpath, "save")
cellStat[indexpath] = true
}
}
这是一个简单的项目,但您可以了解如何做。另外,请注意协议定义和用法,这样您就不会错过任何东西。
输出结果是