Swift 表格视图中的复选框按钮
checkbox button in tableview in Swift
我正在尝试在表格视图中创建复选框功能。
这是一个待办事项列表,如果按下复选框,单元格将隐藏并在不同部分显示为已完成。
来自 C#,这非常简单,但在 swift 中甚至没有一个复选框按钮开始:(...
我通过向 IB 中的自定义原型单元格添加一个带有两个图像(选中、未选中)的按钮使其工作。
由于您不能在同一个 viewcontroller/class 中声明 tableView 和 in-cell-button,我不得不将 tableViewCell 子类化。
现在,如何从 didSelectRowAtIndexPath
访问复选框?当我 select 单元格事件触发时,但当我按下同一个单元格中的复选框按钮时,没有任何触发,我无法隐藏单元格。
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...
}
我知道你想要完成什么,但我会用不同的方式来解决它。也许在您的模型中,您可能有一组待处理的任务和另一组已完成的任务。
section的个数可以是2,section 0和1的行数可以分别是第一个和第二个数组的元素个数。
调用didSelectRowAtIndexPath
时,您可以删除第一个数组中该索引处的项目,并将任务添加到第二个数组。那么你必须调用 tableView.reloadData()
.
我知道您只想选择该行并更改其位置,但在 iOS 中,单元格会被重复使用。最好更新数据源然后重新加载 table.
对于复选标记,您可以确保第 0 部分中的项目没有复选标记配件,而第 1 部分中的项目有。在单元格出队后,您可以在 cellForRowAtIndexPath
中设置附件。
我使用下面的示例代码在单元格中实现复选框按钮 (Xcode 7/ Swift 2.0):
-In 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 tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { }
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: NSArray? = chcklstDick
let ttt: NSMutableArray = chcklstsortedCat?.objectAtIndex(indexPath.section) as! NSMutableArray
if ttt.count > 0 {
var img = UIImage()
let j : Int = ttt.objectAtIndex(indexPath.row) as! Int
NSLog("j: %i", j)
if j > 0 {
img = UIImage(named: "checked")!
}else {
img = UIImage(named: "unchecked")!
}
let bttn : UIButton = UIButton(type: UIButtonType.Custom)
bttn.frame = CGRectMake(0, 0, img.size.width, img.size.height)
bttn.setBackgroundImage(img, forState: UIControlState.Normal)
bttn.addTarget(self, action:"chckBttnTapped:eventy:", forControlEvents: UIControlEvents.TouchUpInside)
cell.accessoryView = bttn
}
}else {NSLog("nonononononononno")}
- 在 func chckBttnTapped(sender: AnyObject, eventy event: 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)
}
-In override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: 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)
}
}
希望有用。
我正在尝试在表格视图中创建复选框功能。 这是一个待办事项列表,如果按下复选框,单元格将隐藏并在不同部分显示为已完成。
来自 C#,这非常简单,但在 swift 中甚至没有一个复选框按钮开始:(...
我通过向 IB 中的自定义原型单元格添加一个带有两个图像(选中、未选中)的按钮使其工作。 由于您不能在同一个 viewcontroller/class 中声明 tableView 和 in-cell-button,我不得不将 tableViewCell 子类化。
现在,如何从 didSelectRowAtIndexPath
访问复选框?当我 select 单元格事件触发时,但当我按下同一个单元格中的复选框按钮时,没有任何触发,我无法隐藏单元格。
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...
}
我知道你想要完成什么,但我会用不同的方式来解决它。也许在您的模型中,您可能有一组待处理的任务和另一组已完成的任务。
section的个数可以是2,section 0和1的行数可以分别是第一个和第二个数组的元素个数。
调用didSelectRowAtIndexPath
时,您可以删除第一个数组中该索引处的项目,并将任务添加到第二个数组。那么你必须调用 tableView.reloadData()
.
我知道您只想选择该行并更改其位置,但在 iOS 中,单元格会被重复使用。最好更新数据源然后重新加载 table.
对于复选标记,您可以确保第 0 部分中的项目没有复选标记配件,而第 1 部分中的项目有。在单元格出队后,您可以在 cellForRowAtIndexPath
中设置附件。
我使用下面的示例代码在单元格中实现复选框按钮 (Xcode 7/ Swift 2.0):
-In 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 tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { }
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: NSArray? = chcklstDick
let ttt: NSMutableArray = chcklstsortedCat?.objectAtIndex(indexPath.section) as! NSMutableArray
if ttt.count > 0 {
var img = UIImage()
let j : Int = ttt.objectAtIndex(indexPath.row) as! Int
NSLog("j: %i", j)
if j > 0 {
img = UIImage(named: "checked")!
}else {
img = UIImage(named: "unchecked")!
}
let bttn : UIButton = UIButton(type: UIButtonType.Custom)
bttn.frame = CGRectMake(0, 0, img.size.width, img.size.height)
bttn.setBackgroundImage(img, forState: UIControlState.Normal)
bttn.addTarget(self, action:"chckBttnTapped:eventy:", forControlEvents: UIControlEvents.TouchUpInside)
cell.accessoryView = bttn
}
}else {NSLog("nonononononononno")}
- 在 func chckBttnTapped(sender: AnyObject, eventy event: 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)
}
-In override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: 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)
}
}
希望有用。