Ewerson Ewerson - 6 months ago 34
Swift Question

How remove white space between table rows in swift

I'm trying to remove blank rows from the table.
When a row it is blank, for example, without text, the line has no contents, but creates a space. I would like to eliminate this 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 = ""
}


}

see here the print

Answer

maybe instead of removing that row you should prevent that row from being created. use numberOfRowsInSection to count the items in your array.

however I think if you just want to hide the cell, you can try using cell.hidden in your code:

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 = ""
}
}
Comments