【问题标题】:Custom design of UITableView in iOSiOS中UITableView的自定义设计
【发布时间】: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


【解决方案1】:

要获得该结果,您必须将 containingView 放在自定义 tableViewCell 中,并将半径和白色赋予该 containingView 而不是单元格。

从情节提要开始,添加你的containingViewlabel(我不知道你用的是什么字体):

我决定在您的 tableView 之上添加一个视图以获得该结果,但我认为您也可以使用 titleForHeaderInSectionwillDisplayHeaderView 获得相同的结果。

您的ViewContollerScene 应如下所示:

但您还必须确保将约束分配给您的containingView,否则您将不会得到该结果:

然后确保您设置了identifier

以及您为单元格创建的 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.

}

}

最终结果应该是这样的。如果您想更改containingViews 之间的距离,您只需减少与contentView 的距离。请记住,您所看到的是containingViews 的边界,而不是您的单元格边界,这就是您必须摆脱单元格分隔符的原因。

终于可以在gitHub找到项目了

【讨论】:

    【解决方案2】:

    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 仅当您需要在视图出现后执行的执行逻辑时才删除 viewDidAppear

    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 的外观?如果我们能看到您所看到的,而不仅仅是一个概念,这将有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多