Adding space between cells in a section in swift 2

I need to add a registration so that the space between each cell in each section is fast 2.1

but all I managed to do was add a title for the section that I don't need.

How can I add space between dynamic table cells?

Here is the code to add the header:

    // Set the spacing between sections
    override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }

    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = UIView()
        header.backgroundColor = UIColor.clearColor()
        return header
    }

This creates the table view code:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.tableData.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell  {

    //Table view cells are reused and should be dequeued using a cell identifier.
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

    let rowData = self.tableData[indexPath.row]

    cell.textLabel!.text = rowData.name
    cell.textLabel!.textAlignment = .Right


    if let url  = NSURL(string: rowData.img),
        data = NSData(contentsOfURL: url)
    {
        cell.imageView?.image = UIImage(data: data)
    }



    return cell
    }

Right now, my view of the table is as follows:

enter image description here

update code and table view:

enter image description here

+4
source share
6 answers

You should just set the cell height:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
   return 100 //cell height
}

UPDATE:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
   if indexPath.row % 2 == 0
   {
      return 100 //cell height
   }
   else
   {
      return 5 //space heigh
   }
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell  {

    //Table view cells are reused and should be dequeued using a cell identifier.
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

    if indexPath.row % 2 == 0
    {
        let rowData = self.tableData[indexPath.row/2]

        cell.textLabel!.text = rowData.name
        cell.textLabel!.textAlignment = .Right

        if let url  = NSURL(string: rowData.img),
            data = NSData(contentsOfURL: url)
        {
            cell.imageView?.image = UIImage(data: data)
        }
        else
        {
            cell.imageView?.image = nil
        }
    }
    else
    {
        cell.textLabel!.text = nil
        cell.imageView?.image = nil
    }
    return cell
}
+2
source

. .

...

  • . . , .

  • 2 . .

+5

HeightForRowAtIndexPath " " XView X ...

objective-c: fooobar.com/questions/121391/...... count...

+1

  • rowHeight ( )

  • viewController

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 
     { return 35;//Whatever height you want...}
    
0

: ""; 1- ( ).

  • "padding" (= ) (.. - . ),
  • You cannot add "margin" (= outer space) between them (which will give you two separators between each cell, maybe ...?).

Collection grids in a view , on the other hand, allow extra space between cells.

0
source

Source: https://habr.com/ru/post/1622881/


All Articles