Swift开发iOS--仿微信朋友圈(4)

Swift开发iOS--仿微信朋友圈(4)——cell高度自适应
cell高度设置在

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{
  return 高度
}
本例中,有文字、图片、点赞、评论、时间、头像等高度需要计算。
一、计算文字内容高度

扩展String类,实现根据字号和label的宽度计算高度

extension String{
    
    //MARK:获得string内容高度
    
    func stringHeightWith(fontSize:CGFloat,width:CGFloat)->CGFloat{
        
        let font = UIFont.systemFontOfSize(fontSize)
        
        let size = CGSizeMake(width,CGFloat.max)
        
        let paragraphStyle = NSMutableParagraphStyle()
        
        paragraphStyle.lineBreakMode = .ByWordWrapping;
        
        let attributes = [NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle.copy()]
        
        let text = self as NSString
        
        let rect = text.boundingRectWithSize(size, options:.UsesLineFragmentOrigin, attributes: attributes, context:nil)
        
        return rect.size.height
        
    }//funcstringHeightWith
    
}//extension end

将数据传入方法中,实现这个方法

func cellHeightByData(data:String)->CGFloat{
    
    let content = data
    let height=content.stringHeightWith(13,width: UIScreen.mainScreen().bounds.width - 55 - 10)
    return  height
    
}

二、计算图片内容高度
根据图片个数计算高度。

func cellHeightByData1(imageNum:Int)->CGFloat{
    
    let lines:CGFloat = (CGFloat(imageNum))/3
    var picHeight:CGFloat = 0
    switch lines{
    case 0...1:
        picHeight = 80
        break
    case 1...2:
        picHeight = 155
        break
    case 2...3:
        picHeight = 230
        break
    default:
        picHeight = 0
    }
    return picHeight
    
}

三、计算评论个数与高度
规定每行评论高20

func cellHeightByCommentNum(Comment:Int)->CGFloat{
    return CGFloat(Comment * 20)
}

四、依次传入参数,计算出高度,返回给cell

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{
        var h_content = cellHeightByData(dataItem[indexPath.row]["content"]! as! String)
        let h_image = cellHeightByData1(dataItem[indexPath.row]["imageUrls"]!.count)
        var h_like:CGFloat = 0.0
        let h_comment = cellHeightByCommentNum(goodComm[indexPath.row]["commentName"]!.count)
        if h_content>13*5{
            if !self.selectItems[indexPath.row]{
                h_content = 13*5
            }
        }
        if goodComm[indexPath.row]["good"]!.count > 0{
            h_like = 40
        }
        return h_content + h_image + 50 + 20 + h_like + h_comment
    }

如果操作中需要改变cell的高度,需要在设置完cell高度后reloadData()当前的TableView。
github传送门

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

推荐阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,098评论 3 38
  • 2017.02.22 可以练习,每当这个时候,脑袋就犯困,我这脑袋真是神奇呀,一说让你做事情,你就犯困,你可不要太...
    Carden阅读 1,389评论 0 1
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,222评论 30 472
  • 补上耿直侃爷给的采访录,他竟然写了一个Word给我,如此认真,吓了我一跳。 1.在我们一般人眼里,银行工作是很稳当...
    广陵十四阅读 377评论 8 2
  • 一个南非回来的诗人朋友,用南非人的简洁,写了一首小诗,末尾一句就是:在南非,人们告诉你,不要用目光直视阳光和人心....
    六职司牧阅读 1,533评论 1 1