当前单元格展开时折叠其他UITableViewCell

时间:2017-10-03 05:27:06

标签: ios swift uitableview

我正在尝试扩展我的UITableViewCell,我可以扩展单元格。但我想要删除未选中的UITableViewCell

我在代码中尝试的是什么:

var expandedCells = [Int]()
@IBOutlet weak var tableVieww:UITableView!
@IBAction func buttonPressed(_ sender: AnyObject) {
    // If the array contains the button that was pressed, then remove that button from the array
    if expandedCells.contains(sender.tag) {
        expandedCells = expandedCells.filter({ $0 != sender.tag})
    }
        // Otherwise, add the button to the array
    else {
        expandedCells.append(sender.tag)
    }
    // Reload the tableView data anytime a button is pressed
    tableVieww.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 20

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! exampleCell
    cell.myButton.tag = indexPath.row
    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    // Set the row height based on whether or not the Int associated with that row is contained in the expandedCells array
    if expandedCells.contains(indexPath.row) {
        return 212
    } else {
        return 57
    }
}

3 个答案:

答案 0 :(得分:1)

您可以维护一个变量来维护所选索引,如下所示

var expandedIndexPath: IndexPath?

然后按如下所示更新tableView委托,

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    expandedIndexPath = indexPath // Update the expandedIndexPath with the selected index
    tableView.reloadData() // Reload tableview to reflect the change
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    // Check wether expanded indexpath is the current index path and updated return the respective height
    if expandedIndexPath == indexPath {
        return 212
    } else {
        return 57
    }
}

这应该可以正常工作。

答案 1 :(得分:1)

1)首先创建Bool数组变量

var isExpandArr = Array<Bool>()

2)在ViewLoad()

中的0索引处插入true
isExpandArr.insert(true, at: 0)

3)放入cellForRowAt

cell.ToggleBT.addTarget(self, action: #selector(handleToggleBtn), for:.touchUpInside)
        cell.ToggleBT.tag = indexPath.row
if isExpandArr[indexPath.row] == true{

             cell.bottomrView.isHidden = false
        }else{

             cell.bottomrView.isHidden = true
        }

4)设置heightForRowAt

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        return isExpandArr[indexPath.row] ? 204 : 60

    }

5)放功能

func handleToggleBtn(sender: UIButton){

        print(sender.tag)

        if isExpandArr.contains(true){

            print("yes")
            if let unwrappedIndex = isExpandArr.index(of: true){

                isExpandArr[unwrappedIndex] = false
                isExpandArr[sender.tag] = true
            }
        }
        table.reloadData()
    }

答案 2 :(得分:0)

在#swift 2.3中 希望这也会有所帮助 折叠并展开标题

在viewDidLoad之上或在类块

中定义变量
var headerIcon: UIImageView = UIImageView()
var sectionTitleArray : NSMutableArray = NSMutableArray()
var sectionContentEventTitleDict : NSMutableDictionary = NSMutableDictionary()
var sectionEventImagesDict : NSMutableDictionary = NSMutableDictionary()
 var arrayForBool : NSMutableArray = NSMutableArray()
var arrFirstSectionItemTitle = ["Section 1 item Name 1","Section 1 item Name 2","Section 1 item Name 3","Section 1 item Name 4","Section 1 item Name 5","Section 1 item Name 6","Section 1 item Name 7"]
var arrFirstSectionItemImages = ["Section_1_icon_1","Section_1_icon_2","Section_1_icon_3","Section_1_icon_4","Section_1_icon_5","Section_1_icon_6","Section_1_icon_7"]
var arrSecondSectionItemTitle = ["Section 2 item Name 1","Section 2 item Name 2","Section 2 item Name 3","Section 2 item Name 4"]
 var arrSecondSectionItemImages = ["Section_1_icon_1","Section_2_item_icon_2","Section_2_item_icon_3","Section_1_item_icon_4","Section_1_item_icon_5","Section_1_item_icon_6","Section_1_item_icon_7"]
var arrData: Array<Dictionary<String,AnyObject>> = Array<Dictionary<String,AnyObject>>()
@IBOutlet weak var tableList: UITableView!

在viewDidLoad()方法中编写这些代码行

tableList.delegate = self
tableList.dataSource = self
 tableList.backgroundColor = UIColor.getRGBColor(240, g: 240, b: 240)
    let customView = UIView(frame: CGRectMake(0, 0, 200, 80))
    customView.backgroundColor = UIColor.getRGBColor(240, g: 240, b: 240)
    let customLine = UIView(frame: CGRectMake(0, 0, tblAllQuestion.frame.size.width, 1))
    customLine.backgroundColor = UIColor.getRGBColor(240, g: 240, b: 240)
    customView.addSubview(customLine)
    tableList.tableFooterView = customView

现在编写委托和数据源方法如下:  // MARK:UITableViewDelegate AND UITableViewDataSource METHOD WITH SECTION

要折叠和展开的部分数量

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    print_debug(sectionTitleArray.count)
    return sectionTitleArray.count
}

细胞方法:

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("CELL_IDENTIFIER", forIndexPath: indexPath)as! CellAllQuestionFiqhVC
    //let cellIdentifier = "CellAllQuestionFiqhVC"
   // cell = self.tableList.dequeueReusableCellWithIdentifier(cellIdentifier)
    let manyCells : Bool = arrayForBool .objectAtIndex(indexPath.section).boolValue
    if (!manyCells) {
        //  cell.textLabel.text = @"click to enlarge";
    }
    else{
                let group = self.arrData[indexPath.section]
                let data: Array<Dictionary<String,AnyObject>> = group["data"]as! Array<Dictionary<String,AnyObject>>
                cell.configCell(data[indexPath.row], instance: self)
    }
    return cell
}

部分中的数字单元格:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if(arrayForBool.objectAtIndex(section).boolValue == true){
        let count1 = arrData[section]["data"]!
        if count1.count != nil {
            return count1.count
        }
    }
    return 0;
}

部分的高度

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 40
}

细胞的高度:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if(arrayForBool.objectAtIndex(indexPath.section).boolValue == true){
        return 100
    }
    return 2;
}

设置标头tappable:

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40))
    headerView.backgroundColor = UIColor.whiteColor()//Constant.kAPP_COLOR
    headerView.tag = section
    let headerString = UILabel(frame: CGRectMake(40/*tableView.frame.size.width/2-30*/, 10, tableView.frame.size.width, 18))
let headerLine = UIView(frame: CGRectMake(0, 39, tableView.frame.size.width, 1))
//UILabel(frame: CGRect(x: 20, y: 10, width: tableView.frame.size.width-10, height: 30)) as UILabel
    //headerLine.text = sectionTitleArray.objectAtIndex(section) as? String
headerLine.backgroundColor = UIColor.getRGBColor(240, g: 240, b: 240)
    headerView .addSubview(headerLine)
headerIcon = UIImageView(frame: CGRect(x: tblAllQuestion.bounds.maxX-30, y: 10, width: 8, height: 12)) as UIImageView
    headerIcon.image = UIImage(named: "right_arrow")
    headerView.addSubview(headerIcon)
    let headerTapped = UITapGestureRecognizer(target: self, action: #selector(sectionHeaderTapped))
    headerView.addGestureRecognizer(headerTapped)
    return headerView
}

点击旋转部分图标:

func rotateSectionIconOnTapped(indexTapped: Int) {
    isActiveMap = false
    if arrayForBool.objectAtIndex(indexTapped) as! NSObject == true {
        print("toggle the icon at degree 90")
        headerIcon.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
    }
    else if arrayForBool.objectAtIndex(indexTapped) as! NSObject == false {
        print("toggle the ico at degree 0")
        headerIcon.transform = CGAffineTransformMakeRotation(0)
    }
}
点击标题标题的

操作:

func sectionHeaderTapped(recognizer: UITapGestureRecognizer) {
    //filterByCategoryIsOn = false
    print("Tapping working")
    print(recognizer.view?.tag)
    //if recognizer.view!.tag != 2 {
        let indexPath : NSIndexPath = NSIndexPath(forRow: 0, inSection:(recognizer.view?.tag as Int!)!)
        if (indexPath.row == 0) {
            for (indexx, bl) in arrayForBool.enumerate() {
                if recognizer.view!.tag != indexx {
                    let index : NSIndexPath = NSIndexPath(forRow: 0, inSection:(indexx))
                    arrayForBool.replaceObjectAtIndex(index.section, withObject: false)
                    let range = NSMakeRange(index.section, 1)
                    let sectionToReload = NSIndexSet(indexesInRange: range)
                    self.tableList.reloadSections(sectionToReload, withRowAnimation:UITableViewRowAnimation.Fade)
                }
            }
            var collapsed = arrayForBool.objectAtIndex(indexPath.section).boolValue
            print_debug(collapsed)
            collapsed    = !collapsed;
            print_debug(collapsed)
            arrayForBool.replaceObjectAtIndex(indexPath.section, withObject: collapsed)
            print(arrayForBool)
            //reload specific section animated
            let range = NSMakeRange(indexPath.section, 1)
            let sectionToReload = NSIndexSet(indexesInRange: range)
            self.tableList.reloadSections(sectionToReload, withRowAnimation:UITableViewRowAnimation.Fade)
            rotateSectionIconOnTapped(recognizer.view!.tag)
        }
}

在索引路径处获取didselect行的操作:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    print_debug(indexPath)
    print_debug(indexPath.row)
    print_debug(indexPath.section)
    if indexPath.section == 0 {

    }
    else if indexPath.section == 1 {

    }
}