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)
        })
        
    }
    
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 2017.02.22 可以練習,每當這個時候,腦袋就犯困,我這腦袋真是神奇呀,一說讓你做事情,你就犯困,你可不要太...
    Carden閱讀 1,378評論 0 1
  • 首先提一些題外話,如果你還不知道“AsyncDisplayKit”或者叫“Texture”是干啥的,請度娘“Asy...
    Q海龍閱讀 7,533評論 10 26
  • AsyncDisplayKit AsyncDisplayKit 是 Facebook 開源的一個用于保持 iOS ...
    二斤寂寞閱讀 6,226評論 0 7
  • AsyncDisplayKit的使用 一、簡介 AsyncDisplayKit 是一個UI框架,最初誕生于 Fac...
    風ai翔閱讀 10,823評論 0 33
  • 我們在上一篇《通過代碼自定義不等高cell》中學習了tableView的相關知識,本文將在上文的基礎上,利用sto...
    啊世ka閱讀 1,538評論 2 7