AsyncDisplayKit

自动计算Text高度:

    let textLabel = ASTextNode()
    textLabel.attributedText = NSAttributedString(string: "自动计算宽高的文字", attributes: [NSForegroundColorAttributeName:UIColor.cyan,
        NSFontAttributeName:UIFont.systemFont(ofSize: 18)])
    textLabel.backgroundColor = UIColor.darkGray
    //text的最小占用宽高
    let CGsizeZero = CGSize(width: 0, height: 0)
    //text的最大占用宽高
    let CGsizeMax = CGSize(width: view.bounds.width, height: CGFloat.greatestFiniteMagnitude)
    let asRange = ASSizeRange(min: CGsizeZero, max: CGsizeMax)
    textLabel.layoutThatFits(asRange)
    //获得text的总高度
    print(textLabel.calculatedSize.height)
    textLabel.frame = CGRect(x: 0, y: 0, width: textLabel.calculatedSize.width, height: textLabel.calculatedSize.height)
    view.addSubview(textLabel.view)

ASTableNode:

class ViewController: UIViewController,ASTableDelegate,ASTableDataSource{

    let asTableNode = ASTableNode()

    let mockData = [
    [NSURL(string: "http://tp1.sinaimg.cn/3985473000/180/5742244430/0")!, "杨大大117", "不论我们最后生疏到什么样子 要记住 曾经对你的好都是真的 ❤"],
    [NSURL(string: "http://tp3.sinaimg.cn/2466802634/180/5740492182/0")!, "孟矾矾", "温和而坚定。"],
    [NSURL(string: "http://tp2.sinaimg.cn/1736940353/180/5634177627/0")!, "郭德欣", "广州交通电台FM106.1主持人"],
    [NSURL(string: "http://tp4.sinaimg.cn/2379086631/180/40052837834/0")!, "我是你景儿", "店铺已更名为:JiLovèng 。大家可以在淘宝宝贝直接搜索这个,不分大小写。[心]jiloveng"]
]

override func viewDidLoad() {
    super.viewDidLoad()
    asTableNode.delegate = self
    asTableNode.dataSource = self
    view.addSubview(asTableNode.view)

func tableNode(_ tableNode: ASTableNode, numberOfRowsInSection section: Int) -> Int {
    return mockData.count
}

func tableNode(_ tableNode: ASTableNode, nodeForRowAt indexPath: IndexPath) -> ASCellNode {
    let cellNode = TestCellNode()
    if indexPath.row < mockData.count {
        let item = mockData[indexPath.row]
        if let URL = item[0] as? NSURL,
            let title = item[1] as? String,
            let subTitle = item[2] as? String {
            cellNode.configureData(iconURL: URL, title: title, subTitle: subTitle)
        }
    }
    return cellNode
    }
}

ASCellNode:

class TestCellNode: ASCellNode {

let iconImageNode = ASImageNode()
let titleLabel = ASTextNode()

override init() {
    super.init()
    addSubnode(iconImageNode)
    addSubnode(titleLabel)
}

override func layout() {
    iconImageNode.frame = CGRect(x: 10, y: 10, width: 50, height: 50)
    titleLabel.frame = CGRect(x: 70, y: 10, width: titleLabel.calculatedSize.width, height: titleLabel.calculatedSize.height)
}

override func calculateSizeThatFits(_ constrainedSize: CGSize) -> CGSize {
    titleLabel.backgroundColor = UIColor.darkGray
    //text的最小占用宽高
    let CGsizeZero = CGSize(width: 0, height: 0)
    //text的最大占用宽高
    let CGsizeMax = CGSize(width: constrainedSize.width - 80, height: CGFloat.greatestFiniteMagnitude)
    let asRange = ASSizeRange(min: CGsizeZero, max: CGsizeMax)
    titleLabel.layoutThatFits(asRange)
    
    if titleLabel.calculatedSize.height < 50 {
        return CGSize(width: constrainedSize.width, height: 70)
    }else {
        return CGSize(width: constrainedSize.width,
                      height: 10 + titleLabel.calculatedSize.height + 10)
    }    
}

func configureData(iconURL: NSURL, title: String, subTitle: String) {
    
    // iconURL:图像接口,暂时用背景色替代
    iconImageNode.backgroundColor = UIColor.cyan
    titleLabel.attributedText = NSAttributedString(string: title, attributes: [
        NSForegroundColorAttributeName: UIColor.black,
        NSFontAttributeName: UIFont.systemFont(ofSize: 18)
        ])
    //  subTitleLabel:
    
    }
}

点击Cell展开

func tableNode(_ tableNode: ASTableNode, didSelectRowAt indexPath: IndexPath) {
    guard let cell = tableNode.nodeForRow(at: indexPath) as? TestCellNode else {
        return
    }
    //取消点击效果
    tableNode.deselectRow(at: indexPath, animated: true)
    
    UIView.animate(withDuration: 0.3) {
        self.asTableNode.performBatchUpdates({
           
            let cellHeight =  cell.frame.height
            if cell.isExpanded {
                //如果是展开状态,点击缩回
                cell.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: cellHeight - 50)
                //如果是正常状态,点击展开
            } else {
                cell.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: cellHeight + 50)
            }
            
            cell.isExpanded = !cell.isExpanded
            
//          滑动到点击的cell
//          self.asTableNode.scrollToRow(at: indexPath, at: UITableViewScrollPosition.top, animated: true)
            
        }, completion: { (success) in
            print(success)
        })
        
    }
    
}

你可能感兴趣的:(AsyncDisplayKit)