iOS中UITableView的定制设计
我正在练习快速编码,因为我是一名设计师,所以我想为我的应用程序定制设计。这是一个简单的列表应用程序。它有两个视图。第一个视图是带有我的待办事项的TableView,第二个视图是关于添加新项目的 我的手机是圆形的,有边框颜色。如何设置单元格的样式?我遇到了这么多的家庭教师,但我还没有找到答案 我听说可以通过设计原型细胞来实现,但我真的不知道怎么做。我搜索了所有地方,但没有发现任何东西可以解释我的问题 这是我的设计: 请你给我引路好吗 到目前为止,我的代码是:iOS中UITableView的定制设计,ios,xcode,uitableview,Ios,Xcode,Uitableview,我正在练习快速编码,因为我是一名设计师,所以我想为我的应用程序定制设计。这是一个简单的列表应用程序。它有两个视图。第一个视图是带有我的待办事项的TableView,第二个视图是关于添加新项目的 我的手机是圆形的,有边框颜色。如何设置单元格的样式?我遇到了这么多的家庭教师,但我还没有找到答案 我听说可以通过设计原型细胞来实现,但我真的不知道怎么做。我搜索了所有地方,但没有发现任何东西可以解释我的问题 这是我的设计: 请你给我引路好吗 到目前为止,我的代码是: import UIKit clas
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
我已经将所有选项设置为背景色为蓝色,色调为白色,当我进入模拟时,单元格文本的字体为黑色。如果有人知道如何解决这个问题,现在就告诉我。1您的数据源;始终尝试先将单元格出列,如果未出列,则分配一个新的单元格
// Data in cells
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell") as? UITableViewCell
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, 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
}
2仅当您的执行逻辑需要在视图出现后执行时,才能删除视图
3尝试在viewDidLoad中设置TableView的颜色,看看这是否会改变什么
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
listItems = savedItems.arrayForKey("UserInputs") as! [NSString]
tableView.backgroundColor = UIColor.blueColor()
tableView.reloadData()
}
您还可以使用TableView的外观更新use吗?如果我们能看到您所看到的,而不仅仅是一个概念,这将很有帮助。要获得该结果,您必须将
包含视图
放在自定义TableView单元格中,并为该包含视图
而不是单元格指定半径和白色
从情节提要开始,添加您的包含视图
和标签
(我不知道您使用的字体):
我已决定在您的表视图
上添加一个视图以获得该结果,但我认为您也可以使用标题或标题部分
和将显示标题视图
,以获得相同的结果
您的ViewContollerScene
应该如下所示:
但您还必须确保将约束指定给您的containingView
,否则将无法获得该结果:
然后确保设置了标识符
以及为单元格创建的customClass
代码如下:
ToDoCustomTableViewCell
import UIKit
import QuartzCore
class ToDoCustomTableViewCell: UITableViewCell {
@IBOutlet weak var containingView: UIView!
@IBOutlet weak var customLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
containingView.layer.borderWidth = 2
containingView.layer.borderColor = UIColor.whiteColor().CGColor
containingView.layer.cornerRadius = 20
containingView.layer.masksToBounds = true
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
和ViewController
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
var listItems = ["item One" ,"item One","item One","item One","item One","item One" ]
@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 = tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as! ToDoCustomTableViewCell
cell.customLabel.text = listItems[indexPath.row]
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
最终结果应该是这样。如果要更改包含视图之间的距离
,只需缩短与内容视图之间的距离即可。请记住,您看到的是包含视图的边框,而不是单元格边框,这就是您必须去掉单元格分隔符的原因
最后,您可以在上找到该项目,我建议您先试一试,然后发布问题,为您指出正确的开始方向,to Do&Add可以分别作为页眉和页脚视图返回。要生成所需的单元格,可以使用Xcode的Interface Builder自定义TableCell以获得所需的外观。我已成功设置了边框颜色和半径,但单元格之间的边距如何?这是可能的吗?理想情况下,你提出的问题可以通过快速搜索Stack Overflow或Google来回答。所以通常是一个“我有问题X”或“我看到一个奇怪的行为Y”的地方,而不是一个你想要的教程。如果你达到了某一点,你试图实现某件事,但却无法实现,那么展示你正在编写的代码,在你尝试过的地方,人们会为你指出正确的方向。我在谷歌上搜索了很多,到目前为止,没有一个有效的答案。但是我用一个代码更新了我的问题。边框不是单元格边框,而是放在单元格内的视图。我会用手机。你的应用程序中有情节提要吗?今天晚些时候我会回答这个问题。