2016-04-17 25 views
5

Tôi có một bảng với một số tùy chỉnh.Cách thêm khoảng cách giữa UITableViewCells - Swift

Photo of my tableView

Đây là mã của tôi:

import UIKit 

class ViewController: UIViewController, UITableViewDelegate { 
    var exercises : [String] = ["Swimming", "Running", "Weight Lifting", "Biking", "Climbing"] 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view, typically from a nib. 
    } 

    //Necessary for basic tableView setup. Defines number of rows in a specific section. 
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ 
     //Setting the amount of rows to the number of elements in exercises. This function returns that. 
     tableView.backgroundColor = UIColor.clearColor() 
     return exercises.count 

    } 

    //Necessary for basic tableView setup. Helps us out content for every cell in the index path. Runs = rows 
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ 



     tableView.separatorColor = UIColor.clearColor() 

     //Setting the footer to default so the extra junk does not show 
     tableView.tableFooterView = UIView() 

     //This will be returned. This automatically creates a prototype cell 
     var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell") 

     //Setting every cell to the respective item in exercises 
     cell.textLabel?.text = exercises[indexPath.row] 
     cell.textLabel?.font = UIFont(name: "Avenir-Light", size: 17) 
     cell.textLabel?.textColor = UIColor.whiteColor() 
     cell.textLabel?.textAlignment = .Center 

     //Border Code 
     cell.layer.borderWidth = 2.0 
     cell.layer.borderColor = UIColor.whiteColor().CGColor 

     //Round Corners 
     cell.layer.cornerRadius = 20 




     return cell 

    } 
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
     cell.backgroundColor = UIColor.clearColor() 

    } 


    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


} 

Tôi muốn có một số khoảng cách giữa mỗi UITableViewCell. Tôi đã thử các cách sau:

  1. Thay đổi chiều cao của mỗi hàng. Tùy chọn này không hoạt động vì tôi có đường viền. Thêm chiều cao hơn chỉ làm cho mỗi hàng trông lớn hơn.

  2. Chuyển đổi từng hàng thành một phần và sau đó sử dụng heightForHeader in section.The post. Tôi muốn tránh tùy chọn này bởi vì tôi sẽ phải chuyển đổi tất cả các hàng của tôi thành các phần.

  3. Thêm UIView trong suốt trong mỗi hàng. Một lần nữa, tùy chọn này không hoạt động vì tôi có đường viền.

Còn cách nào khác không?

Cảm ơn

+0

Tất cả ba tùy chọn thực sự hoạt động nếu bạn làm đúng. Tăng kích thước của ô và không thêm đường viền vào ô nhưng tới chế độ xem bên trong ô. – Sulthan

+0

Xin chào tất cả, cảm ơn sự giúp đỡ của bạn. Tôi đã thử sử dụng câu trả lời của @ Suragch từ [bài đăng này] (http://stackoverflow.com/questions/6216839/how-to-add-spacing-between-uitableviewcell). Tôi đã do dự để sử dụng phương pháp này đầu tiên, nhưng nó đã làm việc khá tốt. –

Trả lời

0

Tôi đã thử phương pháp Ozgur, nhưng nó đã không làm việc vì tôi có đường viền giữa các ô xem bảng của tôi. Cuối cùng, tôi đã sử dụng câu trả lời từ this post. Hy vọng nó sẽ giúp

2

Trước hết, bạn nên di chuyển tableView mã liên quan ra khỏi tableView:cellForRowAtIndexPath, tốt nhất là để viewDidLoad:

override func viewDidLoad { 
    super.viewDidLoad() 
    tableView.separatorColor = UIColor.clearColor() 
    tableView.tableFooterView = UIView() 
} 

Thứ hai, UITableViewCells là những vật thể tái sử dụng vì vậy họ đang dequeued bởi tableView khi có yêu cầu:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell = tableView.dequeueReusableCellWithIdentifier("Cell") 
    if cell == nil { 
    cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell") 
    } 
    ... 
} 

Vì vấn đề của bạn, bạn nên đặt rowHeight trên tableView

override func viewDidLoad { 
    super.viewDidLoad() 
    ... 
    tableView.rowHeight = 100.0 
} 

hoặc thực hiện tableView:heightForRowAtIndexPath: thay vì:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    return 100.0 
} 

Bạn cũng nên cập nhật biên giới và góc giá trị bán kính textLabel thay vì các tế bào:

//Border Code 
cell.textLabel.layer.borderWidth = 2.0 
cell.textLabel.layer.borderColor = UIColor.whiteColor().CGColor 

//Round Corners 
cell.textLabel.layer.cornerRadius = 20 
+0

Xin chào, cảm ơn câu trả lời nhanh và mô tả. Tôi đã làm tất cả những thay đổi mà bạn muốn tôi làm, và tôi vẫn nhận được điều này: http://s23.postimg.org/81ki7y3uj/Full_Size_Render_2.jpg –

0

Bạn có thể thêm khoảng cách bằng cách tạo chế độ xem bổ sung bên trong ô có chứa nội dung của ô có khoảng cách giữa đầu và cuối. Làm cho màu nền của ô mờ và nó sẽ xuất hiện như thể ô có khoảng trắng ở trên và dưới nó

Các vấn đề liên quan