【发布时间】:2016-01-03 22:21:18
【问题描述】:
我正在练习我的 Swift 编码,因为我是一名设计师,所以我想为我的应用程序进行自定义设计。做列表应用程序很简单。它有 2 个视图。第一个是 TableView 与我的待办事项,第二个视图是关于添加一个新项目。
我的单元格是圆形的并且有边框颜色。如何对单元格进行样式化?我遇到了很多 tuts,但我没能找到答案。
我听说可以通过样式化原型单元格来完成,但我真的不知道怎么做。我到处用谷歌搜索,但我发现没有任何东西可以解释我的问题。
这是我的设计:
你能指导我吗?
到目前为止我的代码:
import UIKit
class FirstViewController: UIViewController, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
// Delete functionality
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
listItems.removeAtIndex(indexPath.row)
}
tableView.reloadData()
}
// This function calculates how many rows our table has according to listItems array
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return listItems.count
}
// Data in cells
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
cell.textLabel?.text = listItems[indexPath.row] as String
cell.backgroundColor = UIColor.clearColor()
cell.layer.borderWidth = 2.0
cell.layer.borderColor = UIColor.whiteColor().CGColor
cell.layer.cornerRadius = 15
tableView.separatorColor = UIColor.clearColor()
// self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 10, 0);
return cell
}
override func viewDidAppear(animated: Bool) {
// Refreshing our table
tableView.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
listItems = savedItems.arrayForKey("UserInputs") as! [NSString]
tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
enter code here
我已将背景颜色的所有选项设置为蓝色,并将色调设置为白色,当我进入模拟时,单元格文本的字体为黑色。如果有人知道如何解决此问题,请立即告诉我。
【问题讨论】:
-
我建议先试一试,然后发布问题,为您指明正确的开始方向,待办事项和添加可以分别作为页眉和页脚视图返回。要根据需要生成单元格,您可以使用 Xcode 的 Interface Builder 自定义 TableCell 以实现您想要的外观。
-
我设法设置了边框颜色和半径,但是单元格之间的边距如何?这可能吗?
-
理想情况下,您可以通过快速搜索 Stack Overflow 或 Google 来回答您提出的问题。 SO 通常是“我有问题 X”或“我看到一个奇怪的行为 Y”的地方,而不是一个似乎是你所追求的教程。如果你达到了你试图实现某事但无法实现的地步,请在你尝试过的地方展示你正在编写的代码,人们会为你指出正确的方向。
-
我用谷歌搜索了很多,到目前为止还没有一个有效的答案。但我已经用代码更新了我的问题。
-
边框不是单元格边框,而是您放在单元格内的视图。我会使用customCell。你的应用程序中有故事板吗?今天晚些时候我会回答这个问题。
标签: ios xcode uitableview