Swift UItableView自定义单元格编程(文档)?

我一直在试图find一些文档/教程/例子。 关于如何在swift中做一个高级的tableview。 但除了无尽的故事板教程之外,我已经空了。

我没有故事板和笔尖这样做。 我还没有find任何超出苹果/糟糕/解释库的文件。

我不是试图解释我正在寻找的东西,而是简单地展示下面的devise图像。

在这里输入图像说明

现在,我显然不是要求你们为我创造这个。 我只是希望你可以给我一个链接到一些文档/教程。 这解释了如何使细胞不同? 以及如何以编程方式定位单元格中的元素。

我一直在寻找细胞的限制,但我找不到任何?

我也研究过原型单元,但是我能find的只是故事板。

我希望你们能向我展示一些类似的东西,一些文档/教程。

另一件重要的事情 我发现任何文档都没有使用故事板。 全部使用了tableViewController。

我正在使用一个UITableView的UIViewController。 不是一个tableViewController,这似乎是如何工作的巨大差异。

现在我只是想要一个原型工作。

这里是我的数据如下:

var objects = NSMutableArray() var dataArray = [ ["stockName":"CTC Media Inc.","action":"sell","stockPrice":12.44], ["stockName":"Transglobal Energy","action":"buy","stockPrice":39.40], ["stockName":"NU Skin Enterprises","action":"buy","stockPrice":4.18] ] 

我只能抓取并显示一个数据。

  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { //return self.stocks.count return dataArray.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell let object = dataArray[indexPath.row] as NSDictionary cell.textLabel?.text = object["stockName"] as? String return cell } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { println("You selected cell #\(indexPath.row)!") } 

但是我对于如何定位这些数据还是无能为力。 如具有特定背景颜色的右侧的UIView。 在UIView中居中放置一个UILabel,在左边用填充添加一个UIView,在单元之间自定义间距。 等等

任何帮助,链接到文档,build议将不胜感激!

编辑:

我尝试在单元格内添加约束。 “cell.view.addConstraints(”

当然,它会抛出一个错误,说“UITableViewCell没有名为视图的成员”。

至于如何做细胞内的约束,我仍然是空白:(

编辑2 – 进展:

我设法得到一个UIView显示使用下面的代码:

  testView.setTranslatesAutoresizingMaskIntoConstraints(false) testView.backgroundColor = UIColor.redColor() cell.addSubview(testView) var viewsDictionary = [ "testView":testView] cell.addConstraints( NSLayoutConstraint.constraintsWithVisualFormat( "H:|-50-[testView]|", options: nil, metrics: nil, views: viewsDictionary)) cell.addConstraints( NSLayoutConstraint.constraintsWithVisualFormat( "V:|[testView]|", options: nil, metrics: nil, views: viewsDictionary)) 

但是,由于某种原因,它只出现在最后一个单元格中,而不是所有单元格?

在这里输入图像说明

我只是玩了一点点。 即使所有的颜色/字体都不太对,这会给你一个很好的起点。 希望它可以帮助你。

 class Stock { var name: String? var action: String? var price: String? init(stockData: [String: AnyObject]) { if let n = stockData["stockName"] as? String { name = n } if let a = stockData["action"] as? String { action = a } if let p = stockData["stockPrice"] as? Float { price = NSString(format: "%.2f", p) } } var backgroundColor: UIColor { if action == "sell" { return UIColor.greenColor() } return UIColor.blueColor() } var typeColor: UIColor { if action == "sell" { return UIColor.blackColor() } return UIColor.purpleColor() } var priceLabelColor: UIColor { if action == "sell" { return UIColor.redColor() } return UIColor.greenColor() } } class StockCell: UITableViewCell { let padding: CGFloat = 5 var background: UIView! var typeLabel: UILabel! var nameLabel: UILabel! var priceLabel: UILabel! var stock: Stock? { didSet { if let s = stock { background.backgroundColor = s.backgroundColor priceLabel.text = s.price priceLabel.backgroundColor = s.priceLabelColor typeLabel.text = s.action typeLabel.backgroundColor = s.typeColor nameLabel.text = s.name setNeedsLayout() } } } override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) backgroundColor = UIColor.clearColor() selectionStyle = .None background = UIView(frame: CGRectZero) background.alpha = 0.6 contentView.addSubview(background) nameLabel = UILabel(frame: CGRectZero) nameLabel.textAlignment = .Left nameLabel.textColor = UIColor.blackColor() contentView.addSubview(nameLabel) typeLabel = UILabel(frame: CGRectZero) typeLabel.textAlignment = .Center typeLabel.textColor = UIColor.whiteColor() contentView.addSubview(typeLabel) priceLabel = UILabel(frame: CGRectZero) priceLabel.textAlignment = .Center priceLabel.textColor = UIColor.whiteColor() contentView.addSubview(priceLabel) } required init(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func prepareForReuse() { super.prepareForReuse() } override func layoutSubviews() { super.layoutSubviews() background.frame = CGRectMake(0, padding, frame.width, frame.height - 2 * padding) typeLabel.frame = CGRectMake(padding, (frame.height - 25)/2, 40, 25) priceLabel.frame = CGRectMake(frame.width - 100, padding, 100, frame.height - 2 * padding) nameLabel.frame = CGRectMake(CGRectGetMaxX(typeLabel.frame) + 10, 0, frame.width - priceLabel.frame.width - (CGRectGetMaxX(typeLabel.frame) + 10), frame.height) } } 

在你的视图控制器

 var stocks: [Stock] = [] override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = UIColor.whiteColor() for stockData in dataArray { var stock = Stock(stockData: stockData) stocks.append(stock) } tableView = UITableView(frame: view.bounds, style: .Grouped) tableView.delegate = self tableView.dataSource = self tableView.separatorStyle = .None tableView.registerClass(StockCell.self, forCellReuseIdentifier: NSStringFromClass(StockCell)) view.addSubview(tableView) } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return stocks.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier( NSStringFromClass(StockCell), forIndexPath: indexPath) as StockCell cell.stock = stocks[indexPath.row] return cell } func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return 70 } 

自定义单元格

 class StockCell: UITableViewCell { let padding: CGFloat = 5 var background: UIView! var typeLabel: UILabel! var nameLabel: UILabel! var priceLabel: UILabel! var stock: Stock? { didSet { if let s = stock { background.backgroundColor = s.backgroundColor priceLabel.text = s.price priceLabel.backgroundColor = s.priceLabelColor typeLabel.text = s.action typeLabel.backgroundColor = s.typeColor nameLabel.text = s.name setNeedsLayout() } } } override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) backgroundColor = UIColor.clearColor() selectionStyle = .None background = UIView(frame: CGRectZero) background.alpha = 0.6 contentView.addSubview(background) nameLabel = UILabel(frame: CGRectZero) nameLabel.textAlignment = .Left nameLabel.textColor = UIColor.blackColor() contentView.addSubview(nameLabel) typeLabel = UILabel(frame: CGRectZero) typeLabel.textAlignment = .Center typeLabel.textColor = UIColor.whiteColor() contentView.addSubview(typeLabel) priceLabel = UILabel(frame: CGRectZero) priceLabel.textAlignment = .Center priceLabel.textColor = UIColor.whiteColor() contentView.addSubview(priceLabel) } required init(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func prepareForReuse() { super.prepareForReuse() } override func layoutSubviews() { super.layoutSubviews() background.frame = CGRectMake(0, padding, frame.width, frame.height - 2 * padding) typeLabel.frame = CGRectMake(padding, (frame.height - 25)/2, 40, 25) priceLabel.frame = CGRectMake(frame.width - 100, padding, 100, frame.height - 2 * padding) nameLabel.frame = CGRectMake(CGRectGetMaxX(typeLabel.frame) + 10, 0, frame.width - priceLabel.frame.width - (CGRectGetMaxX(typeLabel.frame) + 10), frame.height) } } 

我无法对接受的答案发表评论,因为我的名声太低(新用户),这种方式真的很有帮助。

不过,我想提一个细节,我相信在取得单元格的方法,你应该检查新取得的项目零,据我所知,只是假设一个有效的单元格总是会返回只要你使用故事板,事实并非如此。

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCellWithIdentifier( NSStringFromClass(StockCell), forIndexPath: indexPath) as StockCell if cell == nil { cell = StockCell(style: UITableViewCellStyle.Default, reuseIdentifier: NSStringFromClass(StockCell)) } // If extra cell customization is needed do so here return cell; }