Swift中tableview中的复选框按钮
我正在尝试在tableview中创建复选框功能。 这是一个待办事项列表,如果按下复选框,单元格将隐藏,并在其他部分显示为已完成 来自C,这是非常简单的,但在swift中甚至没有一个复选框按钮开始: 我在IB中的一个定制原型单元中添加了一个按钮,该按钮带有两个选中、未选中的图像。 因为不能在同一个viewcontroller/类中声明tableView和in-cell按钮,所以我必须对tableViewCell进行子类化 现在,如何从DidSelectRowatineXpath访问复选框?当我选择单元格时,会触发事件,但当我按下同一单元格中的复选框按钮时,不会触发任何事件,并且我无法隐藏该单元格Swift中tableview中的复选框按钮,swift,checkbox,tableview,tableviewcell,Swift,Checkbox,Tableview,Tableviewcell,我正在尝试在tableview中创建复选框功能。 这是一个待办事项列表,如果按下复选框,单元格将隐藏,并在其他部分显示为已完成 来自C,这是非常简单的,但在swift中甚至没有一个复选框按钮开始: 我在IB中的一个定制原型单元中添加了一个按钮,该按钮带有两个选中、未选中的图像。 因为不能在同一个viewcontroller/类中声明tableView和in-cell按钮,所以我必须对tableViewCell进行子类化 现在,如何从DidSelectRowatineXpath访问复选框?当我选择
var indexTag = checkBoxImage.tag
//this is what I have in TableViewCell class
@IBAction func checkBoxInCell(sender: UIButton) {
checkBoxImage.setImage(UIImage(named:"checked"),forState:UIControlState.Normal)
if isChecked != false {
isChecked = false
cellitemcontent.removeAtIndex(indexTag)
//can't access the cell from here to update the tableview
}
else {
isChecked = true
checkBoxImage.setImage(UIImage(named:"unchecked"),forState:UIControlState.Normal)
}
}
//this is what I have in my FirstViewController that contains the tableview
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
cellitemcontent.removeAtIndex(indexTag)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
//when I press the checkBoxImage button in the cell it doesn't fire this event...
}
我知道你想完成什么,但我会用另一种方式来解决。也许在您的模型中,您可以有一个挂起的任务数组和另一个已完成的任务数组 节数可以是2。第0节和第1节中的行数可以分别是第一个数组和第二个数组中的元素数 调用didSelectRowAtIndexPath时,可以删除第一个数组中该索引处的项,并将任务添加到第二个数组中。然后必须调用tableView.reloadData 我知道您只想拾取该行并更改其位置,但在iOS中,这些单元格可以重复使用。最好更新数据源,然后重新加载表
对于复选标记,您可以确保第0节中的项目没有复选标记附件,而第1节中的项目有。在单元格退出队列后,您可以在cellForRowAtIndexPath中设置附件。我使用下面的示例代码实现单元格Xcode 7/Swift 2.0中的复选框按钮: -在viewDidLoad{}:将每个单元格中的复选框状态保存到.plist文件
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
let fnstring = (documentsPath as String)+"/yr-filename.plist"
let fM = NSFileManager.defaultManager()
//if (let a != (fM.fileExistsAtPath(fnstring))) {
if !(fM.fileExistsAtPath(fnstring)) {
let onlyarr = NSMutableArray()
for var i = 0 ; i < 5; ++i{ // number of sections
var arr = NSArray()
switch i { // number cell in each section
case 0: arr = [Int](count: 12, repeatedValue: 0) // 0 means unchecked box.
case 1: arr = [Int](count: 13, repeatedValue: 0)
case 2: arr = [Int](count: 14, repeatedValue: 0)
case 3: arr = [Int](count: 15, repeatedValue: 0)
case 4: arr = [Int](count: 16, repeatedValue: 0)
default: arr = [Int]()
}
onlyarr.addObject(arr)
}
onlyarr.writeToFile(fnstring, atomically: false)
}
-在func chckbttntappedssender:AnyObject中,eventy事件:AnyObject{}
let touches: NSSet = event.allTouches()!
let touch: UITouch = touches.anyObject()! as! UITouch
let crrntTouchPos : CGPoint = touch.locationInView(self.tableView)
let idxpth: NSIndexPath = self.tableView.indexPathForRowAtPoint(crrntTouchPos)!
if idxpth.row != NSNotFound {
self.tableView(self.tableView, accessoryButtonTappedForRowWithIndexPath: idxpth)
}
-在override func tableViewtableView:UITableView中,accessoryButtonTappedForRowWithIndexPath:NSIndexPath{}
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
let fnstring = (documentsPath as String)+"/yr-filename.plist"
let fM = NSFileManager.defaultManager()
if fM.fileExistsAtPath(fnstring) {
let chcklstDick = NSMutableArray(contentsOfURL: NSURL(fileURLWithPath: fnstring))
let chcklstsortedCat: NSMutableArray? = chcklstDick
let ttt: NSMutableArray = chcklstsortedCat?.objectAtIndex(indexPath.section) as! NSMutableArray
if ttt.count > 0 {
let j : Int = ttt.objectAtIndex(indexPath.row) as! Int
var newimg = UIImage()
if j == 0 {
newimg = UIImage(named: "checked")!
}else {
newimg = UIImage(named: "unchecked")!
}
let cell = tableView.dequeueReusableCellWithIdentifier("cellID", forIndexPath: indexPath)
let bttn : UIButton = UIButton(type: UIButtonType.Custom)
bttn.frame = CGRectMake(0, 0, newimg.size.width, newimg.size.height)
bttn.setBackgroundImage(newimg, forState: UIControlState.Normal)
bttn.addTarget(self, action:"chckBttnTapped:eventy:", forControlEvents: UIControlEvents.TouchUpInside)
cell.accessoryView = bttn
self.tableView.reloadData()
self.tableView.reloadInputViews()
ttt.replaceObjectAtIndex(indexPath.row, withObject: 1 - j)
chcklstsortedCat?.replaceObjectAtIndex(indexPath.section, withObject: ttt)
chcklstsortedCat?.writeToFile(fnstring, atomically: false)
}
}
希望它有用。Hm,这是一种有趣的方法,但当我按下该单元格内的复选框按钮时,仍然无法触发事件,因为只有实际选择的单元格才会触发事件。还有其他我错过的手机事件吗?为什么选中时单元格中的按钮不被视为单元格的一部分?我倾向于将表格视图单元格视为一个通用对象,不知道它包含哪些数据。子类uitableviewcell并向其添加一个取消按钮。如果使按钮可访问,则可以在CellForRowatinex中设置按钮标记:cell.button.tag=indexPath.row。然后创建一个方法:-void button touched:UIButton*sender并在CellForRowatinex中设置按钮的目标。因此,当点击任何按钮时,它将调用buttonTouched,您可以通过sender.tag获取行。换句话说,单元格只知道它在哪一行,而按钮现在也知道了,因为标签。谢谢Josh,-子类uitableviewcell并向其添加一个取消按钮|完成|-设置按钮标记:cell.button.tag=indexPath.row |完成|-创建一个方法:-void buttonTouched:UIButton*发送方不确定如何做-在CellForRowatinex中设置按钮的目标,|完成|。如何创建该方法?抱歉。。。斯威夫特不是客观的。创建函数func button touchedSender:UIButton{}我也遇到了同样的问题。我从迈克·阿卜杜拉那里找到了一个我喜欢的答案,这看起来是个好主意,但我似乎没有足够的知识将其转化为swift。如果我成功了,我会告诉你的。
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
let fnstring = (documentsPath as String)+"/yr-filename.plist"
let fM = NSFileManager.defaultManager()
if fM.fileExistsAtPath(fnstring) {
let chcklstDick = NSMutableArray(contentsOfURL: NSURL(fileURLWithPath: fnstring))
let chcklstsortedCat: NSMutableArray? = chcklstDick
let ttt: NSMutableArray = chcklstsortedCat?.objectAtIndex(indexPath.section) as! NSMutableArray
if ttt.count > 0 {
let j : Int = ttt.objectAtIndex(indexPath.row) as! Int
var newimg = UIImage()
if j == 0 {
newimg = UIImage(named: "checked")!
}else {
newimg = UIImage(named: "unchecked")!
}
let cell = tableView.dequeueReusableCellWithIdentifier("cellID", forIndexPath: indexPath)
let bttn : UIButton = UIButton(type: UIButtonType.Custom)
bttn.frame = CGRectMake(0, 0, newimg.size.width, newimg.size.height)
bttn.setBackgroundImage(newimg, forState: UIControlState.Normal)
bttn.addTarget(self, action:"chckBttnTapped:eventy:", forControlEvents: UIControlEvents.TouchUpInside)
cell.accessoryView = bttn
self.tableView.reloadData()
self.tableView.reloadInputViews()
ttt.replaceObjectAtIndex(indexPath.row, withObject: 1 - j)
chcklstsortedCat?.replaceObjectAtIndex(indexPath.section, withObject: ttt)
chcklstsortedCat?.writeToFile(fnstring, atomically: false)
}
}