AsyncDisplayKit

自動(dòng)計(jì)算Text高度:

    let textLabel = ASTextNode()
    textLabel.attributedText = NSAttributedString(string: "自動(dòng)計(jì)算寬高的文字", 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", "不論我們最后生疏到什么樣子 要記住 曾經(jīng)對(duì)你的好都是真的 ?"],
    [NSURL(string: "http://tp3.sinaimg.cn/2466802634/180/5740492182/0")!, "孟礬礬", "溫和而堅(jiān)定。"],
    [NSURL(string: "http://tp2.sinaimg.cn/1736940353/180/5634177627/0")!, "郭德欣", "廣州交通電臺(tái)FM106.1主持人"],
    [NSURL(string: "http://tp4.sinaimg.cn/2379086631/180/40052837834/0")!, "我是你景兒", "店鋪已更名為:JiLovèng 。大家可以在淘寶寶貝直接搜索這個(gè),不分大小寫。[心]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:圖像接口,暫時(shí)用背景色替代
    iconImageNode.backgroundColor = UIColor.cyan
    titleLabel.attributedText = NSAttributedString(string: title, attributes: [
        NSForegroundColorAttributeName: UIColor.black,
        NSFontAttributeName: UIFont.systemFont(ofSize: 18)
        ])
    //  subTitleLabel:
    
    }
}

點(diǎn)擊Cell展開

func tableNode(_ tableNode: ASTableNode, didSelectRowAt indexPath: IndexPath) {
    guard let cell = tableNode.nodeForRow(at: indexPath) as? TestCellNode else {
        return
    }
    //取消點(diǎn)擊效果
    tableNode.deselectRow(at: indexPath, animated: true)
    
    UIView.animate(withDuration: 0.3) {
        self.asTableNode.performBatchUpdates({
           
            let cellHeight =  cell.frame.height
            if cell.isExpanded {
                //如果是展開狀態(tài),點(diǎn)擊縮回
                cell.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: cellHeight - 50)
                //如果是正常狀態(tài),點(diǎn)擊展開
            } else {
                cell.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: cellHeight + 50)
            }
            
            cell.isExpanded = !cell.isExpanded
            
//          滑動(dòng)到點(diǎn)擊的cell
//          self.asTableNode.scrollToRow(at: indexPath, at: UITableViewScrollPosition.top, animated: true)
            
        }, completion: { (success) in
            print(success)
        })
        
    }
    
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 2017.02.22 可以練習(xí),每當(dāng)這個(gè)時(shí)候,腦袋就犯困,我這腦袋真是神奇呀,一說讓你做事情,你就犯困,你可不要太...
    Carden閱讀 1,490評(píng)論 0 1
  • 首先提一些題外話,如果你還不知道“AsyncDisplayKit”或者叫“Texture”是干啥的,請(qǐng)度娘“Asy...
    Q海龍閱讀 7,778評(píng)論 10 26
  • AsyncDisplayKit AsyncDisplayKit 是 Facebook 開源的一個(gè)用于保持 iOS ...
    二斤寂寞閱讀 7,805評(píng)論 0 6
  • AsyncDisplayKit的使用 一、簡(jiǎn)介 AsyncDisplayKit 是一個(gè)UI框架,最初誕生于 Fac...
    風(fēng)ai翔閱讀 11,035評(píng)論 0 33
  • 我們?cè)谏弦黄锻ㄟ^代碼自定義不等高cell》中學(xué)習(xí)了tableView的相關(guān)知識(shí),本文將在上文的基礎(chǔ)上,利用sto...
    啊世ka閱讀 1,649評(píng)論 2 7

友情鏈接更多精彩內(nèi)容