如何检测tableview外的选定部分?

时间:2016-03-23 06:09:00

标签: ios swift uitableview

在这里,我想检测选择了哪个部分,因为我只想删除所选部分的行。有没有办法确定哪个部分被检测到?这里的行返回已选择的行。同样,我想返回已选择的部分。

func deleteSetUp() {

        let Rows = self.tableView.indexPathsForSelectedRows ?? []
        print("Here rows are \(Rows)")

        if(Rows.isEmpty){         
            self.wishListArr.removeAll()
//          self.dataArray.removeAll()

            self.tableView.reloadSections(NSIndexSet(index: 0), withRowAnimation: .Automatic)
        } else {
            let selectedRows = self.tableView.indexPathsForSelectedRows!
            let deleteSpecificRows = selectedRows.count > 0

            if deleteSpecificRows { 
                let indicesOfItemsToDelete = NSMutableIndexSet()           
                for selectionIndex in selectedRows {
                    indicesOfItemsToDelete.addIndex(selectionIndex.row)
                }
       }
}

3 个答案:

答案 0 :(得分:0)

实施委托

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let headerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40))
        headerView.backgroundColor = UIColor.blueColor()
        headerView.tag = section
        let headerTapped = UITapGestureRecognizer (target: self, action:"sectionHeaderTapped:")
        headerView .addGestureRecognizer(headerTapped)

        return headerView
    }

现在处理点击:

func sectionHeaderTapped(tapped: UITapGestureRecognizer){

        let section = tapped.view?.tag
        print(section) // number of section tapped
}

希望这会对你有所帮助。

答案 1 :(得分:0)

是的,当你打算叫那个

时,你可以得到它

有趣的deleteSetUp()函数只是从tableView委托传递索引路径,并使用该索引路径获取详细信息,即部分和行以处理进一步的操作。

你可以通过两种方式完成这项工作

1

 func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) 
{
       self.deleteSetUp(indexPath)
}
func deleteSetUp(indexpath : NSIndexPath)
{
        let set = NSIndexSet(index: indexpath.section)
        self.tableview.reloadSections(NSIndexSet(index: index.section), withRowAnimation: UITableViewRowAnimation.Fade)
}
  1. 您必须确保一次选择1行

    func deleteSetUp()
    {
        let Rows = self.tableView.indexPathsForSelectedRows
        let index = Rows![0] 
        self.tableview.reloadSections(NSIndexSet(index: index.section), withRowAnimation: UITableViewRowAnimation.Fade)
    }
    

答案 2 :(得分:0)

以下代码未经过测试只需尝试

    func deleteSetUp() {

    let Rows = self.aTableView.indexPathsForSelectedRows ?? []
    print("Here rows are \(Rows)")

    if(Rows.isEmpty){
        self.aTableView.reloadSections(NSIndexSet(index: 0), withRowAnimation: .Automatic)
    }
    else
    {
        let selectedRows = self.aTableView.indexPathsForSelectedRows!
        let deleteSpecificRows = selectedRows.count > 0

        if deleteSpecificRows {
            let indicesOfItemsToDelete = NSMutableIndexSet()

            for selectionIndex in selectedRows {
                indicesOfItemsToDelete.addIndex(selectionIndex.row)
            }
        }

        //indexpath contains both row and section info
        if selectedRows.count > 0 {
            let indecesSectionToDelete = NSMutableIndexSet()
            for selectionIndex in selectedRows {
                indecesSectionToDelete.addIndex(selectionIndex.section) //hear u will get the which section to delete 
            }
        }
    }
}