IOS UITableView 表格嵌套

自定义表格控件: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.height
size.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

你可能感兴趣的:(IOS UITableView 表格嵌套)