Augustyniak / RATreeView

Library providing easy-to-use interface for displaying tree structures on iOS and tvOS.
MIT License
2.49k stars 466 forks source link

add expanded or collapsed icon state? #251

Closed fukemy closed 6 years ago

fukemy commented 6 years ago

Hi, does this feature help user know when cell is expand or collapse by using arrow up/down?

Maxatma commented 6 years ago
///------------------------------------------------
/// @name Working with Expandability
///------------------------------------------------

- (BOOL)isCellForItemExpanded:(id)item;
- (BOOL)isCellExpanded:(UITableViewCell *)cell;

You have these methods, u can check if the item is expanded. Arrow up / down - your configure in your own cell with your own taste :)

fukemy commented 6 years ago

hello. I checked in cellForItem. Here is code:

func treeView(_ treeView: RATreeView, cellForItem item: Any?) -> UITableViewCell {

        let cell = treeView.dequeueReusableCell(withIdentifier: String(describing: CellIdentifier)) as! TreeTableViewCell
        let item = item as! DataObject

        let level = treeView.levelForCell(forItem: item)
        let detailsText = "Number of children \(item.children.count)"
        cell.selectionStyle = .none
        cell.setup(withTitle: item.name, detailsText: detailsText, level: level, additionalButtonHidden: false)

        if !item.hasItem(){
            cell.additionalButton.setImage(UIImage(), for: .normal)
        }else{
            if(treeView.isCellExpanded(cell)){
                cell.additionalButton.setImage(UIImage(named: "ic_arrow_down"), for: .normal)
            }else{
                cell.additionalButton.setImage(UIImage(named: "ic_arrow_right"), for: .normal)
            }
        }

        return cell
    }

    func treeView(_ treeView: RATreeView, willExpandRowForItem item: Any) {
        let cell = treeView.cell(forItem: item) as! TreeTableViewCell
        cell.additionalButton.setImage(UIImage(named: "ic_arrow_right"), for: .normal)
        cell.additionalButton.imageView?.image = UIImage(named: "ic_arrow_right")
        cell.backgroundColor = .red
    }

    func treeView(_ treeView: RATreeView, willCollapseRowForItem item: Any) {
        let cell = treeView.cell(forItem: item) as! TreeTableViewCell
        cell.additionalButton.setImage(UIImage(named: "ic_arrow_down"), for: .normal)
        cell.additionalButton.imageView?.image = UIImage(named: "ic_arrow_down")
    }

that above code is update based on your example

But it still not working, the image not change when expand cell, or collapse cell...

Can u help?

Maxatma commented 6 years ago

@fukemy Sure, can u please share ur test project via github ?

fukemy commented 6 years ago

hello. Thanks you for fast response. After thinking, i decide to save isOpened variable in DataObject, then reload row everytime row expand, collapse. Now it working like expected.

Very lucky that row does not collapse after reload 👍

posted code to other people if need

extension PageTreeData :RATreeViewDelegate, RATreeViewDataSource{

    func treeView(_ treeView: RATreeView, numberOfChildrenOfItem item: Any?) -> Int {
        if let item = item as? DataObject {
            return item.children.count
        } else {
            return self.data.count
        }
    }

    func treeView(_ treeView: RATreeView, heightForRowForItem item: Any) -> CGFloat {
        return 61.5
    }

    func treeView(_ treeView: RATreeView, child index: Int, ofItem item: Any?) -> Any {
        if let item = item as? DataObject {
            return item.children[index]
        } else {
            return data[index] as AnyObject
        }
    }

    func treeView(_ treeView: RATreeView, cellForItem item: Any?) -> UITableViewCell {

        let cell = treeView.dequeueReusableCell(withIdentifier: String(describing: CellIdentifier)) as! TreeTableViewCell
        let item = item as! DataObject

        cell.delegate = self
        cell.item = item

        let level = treeView.levelForCell(forItem: item)
        let detailsText = "Items: \(item.children.count)"
        cell.selectionStyle = .none
        cell.setup(withTitle: item.name, detailsText: detailsText, level: level, additionalButtonHidden: false)

        cell.checkbox.on = item.isSelected

        //set background color
        var backgroundColor: UIColor = .white
        if !item.hasItem() {
            backgroundColor = .white
        } else {
            backgroundColor = .groupTableViewBackground
        }

        cell.backgroundColor = backgroundColor
        cell.contentView.backgroundColor = backgroundColor

        //set arrow direction
        if !item.hasItem(){
            cell.additionalButton.setImage(UIImage(), for: .normal)

        }else{
            if(!item.isOpen){
                cell.additionalButton.setImage(UIImage(named: "ic_arrow_right"), for: .normal)
            }else{
                cell.additionalButton.setImage(UIImage(named: "ic_arrow_down"), for: .normal)
            }
        }

        return cell
    }

    func treeView(_ treeView: RATreeView, didExpandRowForItem item: Any) {
        (item as! DataObject).isOpen = true
        treeView.reloadRows()
    }

    func treeView(_ treeView: RATreeView, didCollapseRowForItem item: Any) {
        (item as! DataObject).isOpen = false
        treeView.reloadRows()
    }

}

I think i need to use your lib many time in future, then may be i can open more issue if I can solve :). Just wish u still maintain this project