在自定义单元格类中设置标签,然后使用cellForRow 向其发送数据。
import UIKit
class cellClass: UITableViewCell {
@IBOutlet weak var containerView: UIView!
func addViews(jsonStuff: [String]){
let rect = CGRect(x: 0, y: 0, width: 100, height: 20)
var x = 50 as CGFloat
let y = 20.0 as CGFloat
var position = CGPointMake(x, y)
for str in jsonStuff{
let label = UILabel(frame: rect)
label.text = str
label.center = position
containerView.addSubview(label)
x = x + 40
//y = y + 20
position = CGPointMake(x,y)
}
}
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var table: UITableView!
var stringStuff = ["this", "that", "tje", "blah", "hey"]
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return stringStuff.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! cellClass
if indexPath.row < 2{
let sub = stringStuff[0...2]
cell.addViews(Array(sub))
}else{
cell.addViews(stringStuff)
}
return cell
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
}
这可能是一个巨大的 hack,我相信还有更好的方法,但这里有一些功能代码可以动态调整行大小,而不使用自动布局或预定义标签。您可能必须使用矩形高度才能使所有内容看起来正确,并且 cellForRowAtIndexPath 中的删除子视图对于单元重用是必需的。
class cellClass: UITableViewCell {
@IBOutlet weak var containerView: UIView!
func addViews(lbsToAdd: Int) {
let rctHeight = Int((5 + (lbsToAdd * 20)))
let rect = CGRect(x: 6, y: 10, width: Int(containerView.frame.width-12), height: rctHeight)
containerView.backgroundColor = UIColor.lightGrayColor()
let view1 = UIView(frame: rect)
view1.layer.shadowColor = UIColor.blackColor().CGColor
view1.layer.masksToBounds = false
view1.layer.shadowOpacity = 0.2
view1.layer.shadowOffset = CGSizeMake(-1, 1)
view1.layer.cornerRadius = 2
view1.backgroundColor = UIColor.whiteColor()
let x = 2 as CGFloat
var y = 0 as CGFloat
for i in 1...lbsToAdd {
let recti = CGRect(x: x, y: y, width: 50, height: 20)
let s = UILabel(frame: recti)
s.text = "\(i)"
y = y + 20
containerView.frame.size.height = containerView.frame.size.height + 20
view1.addSubview(s)
print(i)
}
containerView.addSubview(view1)
}
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var testTable: UITableView!
var stringStuff = ["Jetta", "< Golf >", "Passat", "CC", "Beetle", "Ford", "Chevy", "Nissan"]
let labelsCount = 10
override func viewDidLoad() {
super.viewDidLoad()
testTable.delegate = self
testTable.reloadData()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return stringStuff.count
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let total = ((Int(indexPath.row) + 2) * 2)
return CGFloat(total * labelsCount)
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as? cellClass
cell?.containerView.subviews.forEach({ $0.removeFromSuperview() })
cell?.addViews(indexPath.row + 1)
return cell!
}
}