2016-01-05 125 views
4

增加細胞之間的空間在一節,我需要在每個部分每個單元之間添加填充使空間SWIFT 2.1斯威夫特2

但我設法做的是增加頭部對此我不想節那。

如何在動態表格單元格之間添加空格?

這裏是增加頭部代碼:

// 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 
    } 

下面是創建表視圖代碼:

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 
    } 

現在我的表視圖是這樣的:

enter image description here

更新代碼和表格視圖:

enter image description here

+0

哪裏應該是空間?每個細胞之間? – ChikabuZ

+0

你想增加兩個單元格之間的空間嗎? –

+0

@ChikabuZ我想在兩個單元格之間添加空格 – anonymox

回答

2

你應該只設置單元格的高度:

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 
} 
+0

你的方法只是改變細胞的高度!不添加填充他們! – anonymox

+0

然後使用collectionView或看更新。 – ChikabuZ

+0

謝謝。更新的方法工作。但是單元格之間的空間被文本填充有點問題!並且讓它變得醜陋不知道如何解決它?我已經在上面的文章中放置了新的表格視圖截圖 – anonymox

5

有一個表視圖細胞之間沒有間隔。僅在集合視圖中。

提供給您的選項...

  1. 使用集合視圖來代替。使它看起來像桌子視圖相對容易。使用這個你可以使用項目間隔。

  2. 將單元格的高度增加2個像素,並在每個單元格的頂部或底部添加一個空白區域。這會給他們之間增加空間的錯覺。

+0

感謝您的回答,但我如何實現第二種方法? – anonymox

+0

@anonymox如果你目前身高爲100。然後將其改爲102,但請保留所有觀點。所以圖像仍然從0(頂部)到100(底部)。所以它在底部到單元邊緣的距離將爲2。 – Fogmeister

+0

我已經做了你所說的,但只是細胞的高度改爲102.而所有的內容仍然彼此接近。請注意,我的表格視圖正在動態創建內容 – anonymox

0

有2種方式,以增加高度

  1. 在故事板中選擇你,你的tableview並設置rowHeight時(這更好的套房,爲您實現)在尺寸檢查

  2. 添加此功能在您的視圖控制器

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

thans響應,但它不適合我。我需要像填充不改變單元格高度的東西!因爲我的內容是動態生成的 – anonymox

+0

在故事板,自動佈局中添加頂部和/或底部約束的一種方法...向上級視圖添加imageView .. –

1

您可以使用HeightForRowAtIndexPath或尺寸檢查下改變「行高」在你的tableView XIB ...

或者你可以檢查此鏈接爲目標c:https://stackoverflow.com/a/14726457/4489420 ...在這一點,將根據您的陣列數量創建部分...

0

表視圖單元格是按設計連續的:在兩個連續的單元格之間沒有「空格」只有1點厚的分離線(你可以取消)。

  • 可以添加 「填充」(= 內部空間),每個單元內(即,使它們長高 - 看其他答案),但
  • 不能添加 「保證金」( = 外部空間)他們之間(這會給你兩個分隔符之間的每個單元格,也許......?)。

Collection視圖細胞,在另一方面,允許細胞之間的額外空間。

+0

感謝您的參與,但是如何以編程方式向表格視圖單元格添加填充? – anonymox

+0

和其他人一樣,使用'heightForRowAtIndexPath()'並返回你想要的總高度(content + padding)。 –