最近在学习swift ,做了个简单的朋友圈界面,文字图片高度自适应 ,可调整文字字间距,行间距等,可根据需求自行修改布局等。使用的是Masonry布局,SDPhotoBrowser图片浏览。有需要的同学些可以下载参考,互相学习一下,这是demo地址,下载之后记得修改yjxnewproject-Brdging-Header.h 的路径。
-------------初始化tableview-------------
tableView=UITableView(frame:CGRect(x:0, y:0, width:self.view.bounds.size.width, height:self.view.bounds.size.height), style: .plain)
tableView.dataSource=self;
tableView.register(MainTableViewCell.classForCoder(), forCellReuseIdentifier:"cell")
tableView.delegate=self;
tableView.backgroundColor = UIColor.white
tableView.rowHeight = UITableView.automaticDimension
tableView.separatorInset = UIEdgeInsets.zero
// 设置预估行高 --> 先让 tableView 能滚动,在滚动的时候再去计算显示的 cell 的真正的行高,并且调整 tabelView 的滚动范围
tableView.estimatedRowHeight = 30
view.addSubview(tableView)
cell个数
func tableView(_tableView:UITableView, numberOfRowsInSection section:Int) ->Int{
returnself.dataArray.count;
}
cell样式
func tableView(_tableView:UITableView, cellForRowAt indexPath:IndexPath) ->UITableViewCell{
let model:DataModel=self.dataArray[indexPath.row]as!DataModel
let cell :MainTableViewCell= tableView.dequeueReusableCell(withIdentifier:"cell", for: indexPath)as!MainTableViewCell
cell.getValue(model: model)
returncell
}
效果
![](https://img.haomeiwen.com/i7757864/1f6b0a67627387c7.png)
网友评论