如何删除 swift 中 table 行之间的白色 space
How remove white space between table rows in swift
我正在尝试从 table 中删除空白行。
当一行为空白时,例如,没有文本,该行没有内容,但会创建一个 space。我想消除这个 space?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! ArrayDetailTableViewCell
//configure cell
switch indexPath.row {
case 0:
cell.fieldLabel.text = "1."
cell.valueLabel.text = array.item1
case 1:
if array.item2 != "cod" {
cell.fieldLabel.text = "2."
cell.valueLabel.text = array.item2
} else {
cell.fieldLabel.text = ""
cell.valueLabel.text = ""
default:
cell.fieldLabel.text = ""
cell.valueLabel.text = ""
}
}
在包含 tableView
的 ViewController
的 viewWillLayoutSubview
方法中,将 tableView separatorStyle 更改为 UITableViewSeparatorStyle.None
。我认为这会产生您需要的不同。
要删除 space,一种方法是更改 heightForRowAtIndexPath 中的高度 return。
但是在 heightForRowAtIndexPath 中,我们不能调用 cellForRowAtIndexPath,因为它会变成递归的,应用程序会崩溃。所以我们必须使用我们的数据源来检查哪些元素 return 一个空的高度。
像这样:
class My_UITableView: UITableView, UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate {
var My_Items: [Int: Strings] = [Int: String](); //this is your data source which contains your items.
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if (My_Items.count > indexPath.row){
if let Current_Item = My_Items[indexPath.row] {
if (Current_Item == ""){
return 0; //whatever value you wish to check in your data source item, if it matches your condition send a different height.
}
}
}
return 50;
}
}
也许您应该阻止创建该行,而不是删除该行。使用 numberOfRowsInSection
计算数组中的项目。
不过我认为如果您只想隐藏单元格,可以尝试在代码中使用 cell.hidden
:
switch indexPath.row {
case 0:
cell.fieldLabel.text = "1."
cell.valueLabel.text = array.item1
case 1:
if array.item2 != "cod" {
cell.fieldLabel.text = "2."
cell.valueLabel.text = array.item2
} else {
cell.fieldLabel.text = ""
cell.valueLabel.text = ""
cell.hidden = true
default:
cell.fieldLabel.text = ""
cell.valueLabel.text = ""
}
}
我正在尝试从 table 中删除空白行。 当一行为空白时,例如,没有文本,该行没有内容,但会创建一个 space。我想消除这个 space?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! ArrayDetailTableViewCell
//configure cell
switch indexPath.row {
case 0:
cell.fieldLabel.text = "1."
cell.valueLabel.text = array.item1
case 1:
if array.item2 != "cod" {
cell.fieldLabel.text = "2."
cell.valueLabel.text = array.item2
} else {
cell.fieldLabel.text = ""
cell.valueLabel.text = ""
default:
cell.fieldLabel.text = ""
cell.valueLabel.text = ""
}
}
在包含 tableView
的 ViewController
的 viewWillLayoutSubview
方法中,将 tableView separatorStyle 更改为 UITableViewSeparatorStyle.None
。我认为这会产生您需要的不同。
要删除 space,一种方法是更改 heightForRowAtIndexPath 中的高度 return。
但是在 heightForRowAtIndexPath 中,我们不能调用 cellForRowAtIndexPath,因为它会变成递归的,应用程序会崩溃。所以我们必须使用我们的数据源来检查哪些元素 return 一个空的高度。
像这样:
class My_UITableView: UITableView, UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate {
var My_Items: [Int: Strings] = [Int: String](); //this is your data source which contains your items.
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if (My_Items.count > indexPath.row){
if let Current_Item = My_Items[indexPath.row] {
if (Current_Item == ""){
return 0; //whatever value you wish to check in your data source item, if it matches your condition send a different height.
}
}
}
return 50;
}
}
也许您应该阻止创建该行,而不是删除该行。使用 numberOfRowsInSection
计算数组中的项目。
不过我认为如果您只想隐藏单元格,可以尝试在代码中使用 cell.hidden
:
switch indexPath.row {
case 0:
cell.fieldLabel.text = "1."
cell.valueLabel.text = array.item1
case 1:
if array.item2 != "cod" {
cell.fieldLabel.text = "2."
cell.valueLabel.text = array.item2
} else {
cell.fieldLabel.text = ""
cell.valueLabel.text = ""
cell.hidden = true
default:
cell.fieldLabel.text = ""
cell.valueLabel.text = ""
}
}