如何在UITableViewCell之間添加間距有沒有辦法增加間距UITableViewCell?我創建了一個表,每個單元格只包含一個圖像。圖像被分配給單元格,如下所示:cell.imageView.image = [myImages objectAtIndex:indexPath.row];但這會使圖像放大并適合整個單元格,圖像之間沒有間距?;蛘咭赃@種方式說,圖像的高度例如是50,并且我想在圖像之間添加20個間隔。有沒有辦法實現這個目標?
3 回答
四季花海
TA貢獻1811條經驗 獲得超5個贊
Swift版本
針對Swift 3進行了更新
為了未來的觀眾,這個答案比原始問題更為籠統。它是Swift的基本UITableView示例的補充示例。
概觀
基本思想是為每個數組項創建一個新的部分(而不是一個新行)。然后可以使用截面標題高度來隔開這些截面。
怎么做
按照Swift的UITableView示例中的描述設置項目。(也就是說,添加一個
UITableView并將tableView插座連接到View Controller)。在Interface Builder中,將主視圖背景顏色更改為淺藍色,將
UITableView背景顏色更改為清除。用以下內容替換ViewController.swift代碼。
ViewController.swift
import UIKitclass ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
// These strings will be the data for the table view cells
let animals: [String] = ["Horse", "Cow", "Camel", "Sheep", "Goat"]
let cellReuseIdentifier = "cell"
let cellSpacingHeight: CGFloat = 5
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// These tasks can also be done in IB if you prefer.
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
tableView.delegate = self
tableView.dataSource = self }
// MARK: - Table View delegate methods
func numberOfSections(in tableView: UITableView) -> Int {
return self.animals.count }
// There is just one row in every section
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
// Set the spacing between sections
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return cellSpacingHeight }
// Make the background color show through
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.backgroundColor = UIColor.clear return headerView }
// create a cell for each table view row
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!
// note that indexPath.section is used rather than indexPath.row
cell.textLabel?.text = self.animals[indexPath.section]
// add border and color
cell.backgroundColor = UIColor.white
cell.layer.borderColor = UIColor.black.cgColor
cell.layer.borderWidth = 1
cell.layer.cornerRadius = 8
cell.clipsToBounds = true
return cell }
// method to run when table view cell is tapped
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// note that indexPath.section is used rather than indexPath.row
print("You tapped cell number \(indexPath.section).")
}}請注意,indexPath.section使用而不是indexPath.row為了獲得數組元素和分接位置的正確值。
你是如何在左右兩側獲得額外的填充/空間的?
我得到它的方式與為任何視圖添加間距的方式相同。我使用了自動布局約束。只需使用Interface Builder中的pin工具為前導和尾隨約束添加間距。
瀟瀟雨雨
TA貢獻1833條經驗 獲得超4個贊
我在單元格之間添加間距的方法是使numberOfSections =“你的數組計數”并使每個部分只包含一行。然后定義headerView及其高度。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return yourArry.count;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;}-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return cellSpacingHeight;}-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *v = [UIView new];
[v setBackgroundColor:[UIColor clearColor]];
return v;}
ITMISS
TA貢獻1871條經驗 獲得超8個贊
我使用Swift的簡單解決方案:
// Inside UITableViewCell subclassoverride func layoutSubviews() {
super.layoutSubviews()
contentView.frame = contentView.frame.inset(by: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))}- 3 回答
- 0 關注
- 2233 瀏覽
添加回答
舉報
0/150
提交
取消
