最近项目的社区模块,有需要用到类似于微信的评论回复列表,可是以前没有做过,怎么办? 还能怎么办当然是找度娘了,所幸的是还真被我找到了,在这里贴上 gitHub 地址 : [ https://github.com/suqiang/commentDemo ],不过我现在用的是 Swift3.0编写代码,所以我在 commentDemo的思路下,自己用 Swift实现了该功能,具体思路如下:
首先,我们将接口请求到的数据,用循环的方式,遍历所有的字典数据, 创建一个 ViewModel[ TDCommentCellModel ],该类用于设计 Cell布局,同时设置评论 / 回复 功能
class TDCommentCellModel: NSObject {
var tdMessageModel: MessageCommentModel?///一条评论数据的模型
var commentCellModels: [TDCommentListCellModel]?///存储每一个 cell 的 TDCommentCellModel
///对 cell 各个子 view 元素布局属性
var iconFrame: CGRect?
var nameLabelFrame: CGRect?
var timeFrame: CGRect?
var commentBtnFrame: CGRect?
var contentFrame: CGRect?
var tableviewFrame: CGRect?
///缓存Cell 的高度
var cellHeight: CGFloat = 0
func initWithCommentCellModel(_ model:MessageCommentModel){
//赋值模型
self.tdMessageModel = model
//边缘距离
let margin:CGFloat = 15
//评论框的宽度
let contentW = ScreenWidth-2*margin //
let iconX:CGFloat = 10
let iconY:CGFloat = margin
let iconSize:CGFloat = 37
self.iconFrame = CGRect(x:iconX, y:iconY, width:iconSize, height:iconSize)
let nameLabelX:CGFloat = (self.iconFrame?.maxX)!+margin
let nameLabelW:CGFloat = ScreenWidth-57
let nameLabelH:CGFloat = 14
let timeLabelX = nameLabelX
let timeLabelW:CGFloat = ScreenWidth-57
let timeLabelH:CGFloat = 12
let commentBtnX:CGFloat = ScreenWidth-47
let commentBtnY:CGFloat = iconY
let commentBtnSize:CGFloat = 37
self.commentBtnFrame = CGRect(x:commentBtnX, y:commentBtnY, width:commentBtnSize, height:commentBtnSize)
let commentLabelX:CGFloat = timeLabelX
let commentLabelW:CGFloat = contentW-margin-iconSize
var commentLabelH:CGFloat = 0
///计算评论内容的高度
commentLabelH = getTitleSize(model.content!, titleFont: 14, titleSize: CGSize(width:commentLabelW, height:CGFloat(MAXFLOAT))).height
let tableViewX:CGFloat = commentLabelX
let tableViewW:CGFloat = ScreenWidth-margin-tableViewX
var tableViewH:CGFloat = 0
let commentTitle = model.list!//所有的评论回复数据
var mutableListTitle = [TDCommentListCellModel]()
if commentTitle.count > 0 {// print("111111")
for index in 0..<commentTitle.count {
self.cellHeight += margin
self.nameLabelFrame = CGRect(x:nameLabelX, y:self.cellHeight, width:nameLabelW, height:nameLabelH)
self.cellHeight += nameLabelH
}
if timeLabelH > 0 {
self.cellHeight += margin*0.5
self.timeFrame = CGRect(x:timeLabelX, y:self.cellHeight, width:timeLabelW, height:timeLabelH)
self.cellHeight += timeLabelH
}
if commentLabelH > 0 {
self.cellHeight += margin*0.5
self.contentFrame = CGRect(x:commentLabelX, y:self.cellHeight, width:commentLabelW, height:commentLabelH)
self.cellHeight += commentLabelH
}
// print("出来 for循环之后tableViewH的高度 :\(tableViewH)")
if tableViewH > 0 {
self.cellHeight += margin*0.5
self.tableviewFrame = CGRect(x:tableViewX, y:self.cellHeight, width:tableViewW, height:tableViewH)
self.cellHeight += tableViewH
}else {
self.tableviewFrame = CGRect(x:0, y:0, width:0, height:0)
}
self.cellHeight += margin*0.5
}
}
而其中的 TDCommentListCellModel类,目的在于设置评论回复功能的一些属性,
class TDCommentListCellModel: NSObject {
var maxWidth: CGFloat?
var commentAttributedString: NSMutableAttributedString?
var commentLabelFrame: CGRect?
var cellHeight: CGFloat?
var commentModel: CommentListModel? {
didSet{
let margin:CGFloat = 5
let contentLabelX:CGFloat = margin
let contentLabelY:CGFloat = 4
let contentLabelW:CGFloat = self.maxWidth!-2*margin
var contentLabelH:CGFloat = 0
这一部分的判断依据项目的实际情况和接口返回的数据结构来决定
var contentState: String = ""
if (((commentModel?.replay_name!)! as NSString).length > 0 && ((commentModel?.comment_name!)! != (commentModel?.replay_name!)!)) {
contentState = "\((commentModel?.comment_name!)!)回复\((commentModel?.replay_name!)!): \((commentModel?.content!)!)"
}else {
contentState = "\((commentModel?.comment_name!)!): \((commentModel?.content!)!)"
}
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 4
let attr = [NSParagraphStyleAttributeName:paragraphStyle, NSFontAttributeName:UIFont.systemFont(ofSize: 14), NSForegroundColorAttributeName:UIColor(red:135/255.0, green:135/255.0, blue:135/255.0, alpha:1.0)]
self.commentAttributedString = NSMutableAttributedString(string:contentState, attributes:attr)
contentLabelH = (self.commentAttributedString?.boundingRect(with: CGSize(width:contentLabelW, height:CGFloat(MAXFLOAT)), options: NSStringDrawingOptions.usesLineFragmentOrigin, context: nil).size.height)!
self.commentLabelFrame = CGRect(x:contentLabelX, y:contentLabelY, width:contentLabelW, height:contentLabelH)
self.cellHeight = (self.commentLabelFrame?.maxY)!+2
}
}
}