UITableView 使用 AutoLayout 实现 Cell 点击展开效果


title: UITableView 使用 AutoLayout 实现 Cell 点击展开效果
date: 2017-07-02 13:18:12
tags: [ios, UITableView]
category: ios


效果图

接上篇文章 UITableView Cell 高度自动匹配

关键代码

用到的库:

import SnapKit
import RxSwift
import RxCocoa

使用改变 Label 的 numberOfLines 来实现点击扩展效果

mLabelContent.numberOfLines = 0  // 不限制行数,扩展
mLabelContent.numberOfLines = 2  // 限制函数为 2 行

更新 UITableView 的一个 Cell:

mTableView.rx.itemSelected.subscribe(onNext:{ indexPath in
    
    let cell = self.mTableView.cellForRow(at: indexPath) as! Cell

    self.mTableView.beginUpdates()
    cell.mModel?.isExpand = !(cell.mModel?.isExpand ?? false) // 通过更新数据源的形式改变 cell
    self.mTableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic)           
    self.mTableView.endUpdates()
    
}).disposed(by: mDisposeBag)

完整代码块

Cell:

import Foundation
import SnapKit

class Cell: UITableViewCell{
    
    static let CELL_IDENTIFIER = "Cell"
    
    
    let mLabelTitle = UILabel()
    let mLabelContent = UILabel()
    let mLabelTime = UILabel()
    
    var mModel: MessageModel?{
        didSet{            
            mLabelTitle.text = mModel?.title
            mLabelContent.text = mModel?.content
            mLabelTime.text = mModel?.time
            mLabelContent.numberOfLines = (mModel?.isExpand ?? false) ? 0 : 2
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        
        contentView.addSubview(mLabelTitle)
        contentView.addSubview(mLabelContent)
        contentView.addSubview(mLabelTime)
        
        
        mLabelTitle.snp.makeConstraints { (make) in
            make.top.equalTo(contentView).offset(4)
            make.left.equalTo(contentView).offset(16)
        }
//        mLabelContent.numberOfLines = 0
        mLabelContent.snp.makeConstraints { (make) in
            make.top.equalTo(mLabelTitle.snp.bottom).offset(3)
            make.left.equalTo(contentView).offset(16)
            make.right.equalTo(contentView).offset(-16)
        }
        mLabelTime.snp.makeConstraints { (make) in
            make.top.equalTo(mLabelContent.snp.bottom).offset(3)
            make.bottom.equalTo(contentView).offset(-4)
            make.right.equalTo(contentView).offset(-16)
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

MessageModel:

import Foundation

class MessageModel{
    
    var title: String?
    var content: String?
    var time: String?
    
    var isExpand: Bool?
    
    init() {
        
    }
    
}

AutoHeightViewController:

lazy var mTableView: UITableView = {
    let tv = UITableView()
    tv.separatorStyle = .none
    tv.register(Cell.self, forCellReuseIdentifier: Cell.CELL_IDENTIFIER)
    tv.estimatedRowHeight = 40
    tv.rowHeight = UITableViewAutomaticDimension
    return tv
}()


mTableView.rx.itemSelected.subscribe(onNext:{ indexPath in
    
    let cell = self.mTableView.cellForRow(at: indexPath) as! Cell

    self.mTableView.beginUpdates()
    cell.mModel?.isExpand = !(cell.mModel?.isExpand ?? false) // 通过更新数据源的形式改变 cell
    self.mTableView.reloadRows(at: [indexPath]UITableViewRowAnimation.automatic)            
    self.mTableView.endUpdates()
    
}).disposed(by: mDisposeBag)

初学 ios ,欢迎指教

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,303评论 4 61
  • 1、 作为一个专注单身三十年的程序猿,酸奶兄每次和我上街眼睛都直勾勾的盯着美女看。每当看见美女身边有个又矮又丑的男...
    剑圣喵大师阅读 4,589评论 39 114
  • “我许你一世情缘,让我们不离不弃”犀牛鸟趴在犀牛的耳边轻轻的说道。 自然界中有着很多奇幻而忠诚的爱情故事,...
    叁里人阅读 588评论 0 1
  • 本文参加#未完待续,就要表白#活动,本人承诺,文章内容为原创,且未在其他平台发表过 再见,我爱的你! 恋爱篇 六月...
    朱大鹏就是朱鹏阅读 431评论 1 16
  • 在家休养了一个月,这也是我离家九年在家待的最长的时间。 有一种父母叫希望你保持身材,好好减肥,却总...
    蝶舞芙蓉阅读 490评论 1 4