swif tableview全选

时间:2023-03-08 19:07:32

swif tableview全选

func selctAll() {

idArr.removeAll()

for var i = 0; i<sellingArr.count; i++ {

let path: NSIndexPath = NSIndexPath(forRow: i, inSection: 0)

self.tableView.selectRowAtIndexPath(path, animated: true, scrollPosition: UITableViewScrollPosition.None)

}

}

//取消全选

func cancelSelectAll() {

for var i = 0; i < sellingArr.count; i++ {

let path: NSIndexPath = NSIndexPath.init(forItem: i, inSection: 0)

self.tableView.deselectRowAtIndexPath(path, animated: true)

}

tableView.setEditing(false, animated: true)

}

//代理中除了要实现必须实现的代理以外有些需要注意的地方

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

{

let cell = tableView.dequeueReusableCellWithIdentifier(BSManagerBaseCellID) as! BSManagerTableViewCell

cell.selectedBackgroundView = UIView()// 这句可以淡化选择时的颜色

return cell

}

//编辑时写法,OC中写法相对简单,可以百度需要的OC的同学

override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {

return UITableViewCellEditingStyle(rawValue: UITableViewCellEditingStyle.Delete.rawValue | UITableViewCellEditingStyle.Insert.rawValue)!

}

//选中时在这里操作

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

let path: NSIndexPath = NSIndexPath.init(forItem: indexPath.row, inSection: 0)

let cell = tableView.cellForRowAtIndexPath(path) as! BSManagerTableViewCell

if cell.selected == true {//如果是选中状态的话,我在这里的操作是把选中cell的id放在了一个数组中。

idArr.append((sellingArr[indexPath.row]["id"] as? String)!)

}

}

//取消选中的cell状态

override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {

if isSelectStatus == true {//如果是选中状态的话,我在这里的操作是把选中cell的id从数组中移除。

idArr.removeAtIndex(idArr.indexOf((sellingArr[indexPath.row]["id"] as? String)!)!)

}

}