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
Đâ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:
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.
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.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
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
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. –