前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >IOS UITableView 表格嵌套

IOS UITableView 表格嵌套

作者头像
用户5760343
发布2019-07-08 11:53:08
9670
发布2019-07-08 11:53:08
举报
文章被收录于专栏:sktjsktj

自定义表格控件:CustomizeUITableViewCell.swif

//自定义单元格,单元格高度动态调整
 1 import UIKit
 2
 3 class CustomizeUITableViewCell:UITableViewCell,
 UITableViewDataSource, UITableViewDelegate {
 4
 5 var tableView:UITableView!;
 6 var comments:[String] = []
 7
 8 override init(style:UITableViewCellStyle,
 reuseIdentifier:String?) {
 9
 10 super.init(style:style, reuseIdentifier:
 reuseIdentifier);
 11
 12 tableView = UITableView(frame:CGRect(x:20, y:0,
 width:280, height:90))
 13 tableView.dataSource = self
 14 tableView.delegate = self
 15 tableView.isScrollEnabled = false;
 16
 17 self.addSubview(tableView)
 18 }
 19
 20 func tableView(_ tableView:UITableView,
 numberOfRowsInSection section:Int) -> Int{
 21 return comments.count
 22 }
 23
 24 func tableView(_ tableView:UITableView,
 cellForRowAt indexPath:IndexPath)
 25 -> UITableViewCell {
 26 let identifier = “reusedCell”
 27 var cell =
 tableView.dequeueReusableCell(withIdentifier:identifier)
 28
 29 if(cell == nil){
 30 cell = UITableViewCell(style:
 UITableViewCellStyle.default,
 31 reuseIdentifier:identifier)
 32 }
 33 cell?.textLabel?.text = comments[(indexPath as
 NSIndexPath).row]
 34 cell?.textLabel?.font = UIFont.systemFont(ofSize:
 
35 cell?.textLabel?.textColor = UIColor.gray
 36 cell?.textLabel?.numberOfLines = 0;
 37 return cell!
 38 }
 39
 40 func tableView(_ tableView:UITableView,
 heightForRowAt indexPath:IndexPath)
 41 -> CGFloat {
 42 let subComments = comments[(indexPath as
 NSIndexPath).row]
 43 let size = subComments.boundingRect(with:CGSize(),
 44 options:NSStringDrawingOptions.usesFontLeading,
 attributes:nil, context:nil);
 45 let cellHeight = size.heightsize.width/170
 46 if(cellHeight < 30){
 47 return 30
 48 }else{
 49 return cellHeight
 50 }
 51 }
 52
 53 func setCommentsForTable(_ comments:[String]){
 54 self.comments = comments
 55
 56 var tableHeight:CGFloat = 0
 57 for i in 0 ..< comments.count
 58 {
 59 let size = comments[i].boundingRect(with:CGSize(),
 60 options:NSStringDrawingOptions.usesFontLeading,
 attributes:nil, context:nil);
 61 tableHeight += size.heightsize.width/170
 62 }
 63 tableView.frame = CGRect(x:20, y:0, width:280,
 height:tableHeight + 50)
 64 tableView.reloadData()
 65 }
 66
 67 func getMyHeight()->CGFloat{
 68 return tableView.frame.size.height
 69 }
 70
 71 required init(coder aDecoder:NSCoder) {
 72 fatalError(“init(code:)has not brrn implomented”);
 73 }
 74 }
//主视图控制器:
 import UIKit
 class ViewController:UIViewController,
 UITableViewDataSource, UITableViewDelegate {
 4
 5 var articles = [“微软有哪些「黑历史」?”, “苹果有哪些
 黑科技?”, “巴宝莉和 Apple TV 强强 联手推出天台秀直播”]
 6 var comments = [[“省略的文字”,”省略的文字”],[“省略的
 文字”,”省略的文字”],[“省略的文字”, “省略的文字”]]
 7
 8 override func viewDidLoad() {
 9 super.viewDidLoad()
 10 // Do any additional setup after loading the view,
 typically from a nib.
 11
 12 let screenRect = UIScreen.main.bounds
 13 let tableRect = CGRect(x:0, y:20, width:
 screenRect.size.width, height:screenRect.size.height - 20)
 14 let tableView = UITableView(frame:tableRect)
 15
 16 tableView.dataSource = self
 17 tableView.delegate = self
 18 tableView.separatorStyle =
 UITableViewCellSeparatorStyle.none
 19
 20 self.view.addSubview(tableView)
 21 }
 22
 23 func tableView(_ tableView:UITableView,
 numberOfRowsInSection section:Int) -> Int{
 24 return articles.count * 2
 25 }
 26
 27 func tableView(_ tableView:UITableView,
 cellForRowAt indexPath:IndexPath)
 28 -> UITableViewCell {
 29
 30 let cellForArticle = “cellForArticle”
 31 let cellForComments = “cellForComments”
 32
 33 var cell1:UITableViewCell?;
 34 var cell2:CustomizeUITableViewCell?;
 35
 36 if (indexPath as NSIndexPath).row % 2 == 0{
 37 cell1 =
 tableView.dequeueReusableCell(withIdentifier:
 cellForArticle)
 38 if cell1 == nil{
 39 cell1 = UITableViewCell(style:
 UITableViewCellStyle.default,
 40 reuseIdentifier:cellForArticle)
 41 }
 42 cell1?.textLabel?.text = articles[(indexPath as
 NSIndexPath).row/2]
 43 cell1?.textLabel?.font = UIFont.systemFont(ofSize:
 
44 cell1?.textLabel?.textColor = UIColor.lightGray
 45 cell1?.backgroundColor = UIColor.black
 46 return cell1!
 47 }else{
 48 cell2 =
 tableView.dequeueReusableCell(withIdentifier:
 cellForComments) as? CustomizeUITableViewCell
 49 if cell2 == nil{
 50 cell2 = CustomizeUITableViewCell(style:
 UITableViewCellStyle.default,
 51 reuseIdentifier:cellForComments)
 52 }
 53 let subComments = comments[(indexPath as
 NSIndexPath).row/2]
 54 cell2?.setCommentsForTable(subComments)
 55 return cell2!
 56 }
 57 }
 58
 59 func tableView(_ tableView:UITableView,
 heightForRowAt indexPath:IndexPath) -> CGFloat {
 60 if (indexPath as NSIndexPath).row % 2 == 0{
 61 return 40
 62 }else{
 63 let subComments = comments[(indexPath as
 NSIndexPath).row/2]
 64 var cellHeight:CGFloat = 0
 65 for i in 0 ..< subComments.count
 66 {
 67 let size = subComments[i].boundingRect(with:
 CGSize(), options:
 NSStringDrawingOptions.usesFontLeading, attributes:nil,
 context:nil);
 68 cellHeight += size.height*(size.width/170)
 69 }
 70 return cellHeight + 50
 71 }
 72 }
 73 }
//boundingRectWithSize(_:options:context:)方法的参数说明,字符串区域

image.png

image.png

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.06.02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 自定义表格控件:CustomizeUITableViewCell.swif
相关产品与服务
云直播
云直播(Cloud Streaming Services,CSS)为您提供极速、稳定、专业的云端直播处理服务,根据业务的不同直播场景需求,云直播提供了标准直播、快直播、云导播台三种服务,分别针对大规模实时观看、超低延时直播、便捷云端导播的场景,配合腾讯云视立方·直播 SDK,为您提供一站式的音视频直播解决方案。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档