我一直在努力寻找关于如何在Swift中创建高级TableView的文档/教程/示例,但除了无尽的Storyboard教程外,我找不到什么。
我正在使用没有storyboards和nibs的方式进行开发。除了 Apple 官方 /不太好/解释的库之外,我找不到任何其他文档。
与其试图解释我所寻找的内容,我更愿意直接展示下面设计的图片。
现在,我显然并不是要求你们为我创建这个,我只是希望您能向我提供一个链接,来解释如何使单元格不同?以及如何在单元格内编程定位元素。
我一直在寻找单元格约束,但我找不到任何文档?
我也研究了原型单元格,但我找到的都与Storyboard相关。
我希望你们能向我展示一些类似的示例,或者提供一些相关的文档/教程。
另一个重要的事情是,任何我找到的不使用storyboards的文档都使用tableViewController。
我使用的是UIViewController和UITableView,而不是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等等。
任何帮助、文档链接或建议都将不胜感激!
编辑:
我尝试在单元格内部添加约束条件。"cell.view.addConstraints("
但当然它会抛出一个错误,说"UITableViewCell没有名为view的成员"。
因此,关于如何在单元格内添加约束条件,我仍然一头雾水 :(
编辑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))
然而,由于某些原因,它只出现在最后一个单元格,而不是所有单元格中?