3 回答

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超5個(gè)贊
Swift版本
針對(duì)Swift 3進(jìn)行了更新
為了未來(lái)的觀眾,這個(gè)答案比原始問(wèn)題更為籠統(tǒng)。它是Swift的基本UITableView示例的補(bǔ)充示例。
概觀
基本思想是為每個(gè)數(shù)組項(xiàng)創(chuàng)建一個(gè)新的部分(而不是一個(gè)新行)。然后可以使用截面標(biāo)題高度來(lái)隔開(kāi)這些截面。
怎么做
按照Swift的UITableView示例中的描述設(shè)置項(xiàng)目。(也就是說(shuō),添加一個(gè)
UITableView
并將tableView
插座連接到View Controller)。在Interface Builder中,將主視圖背景顏色更改為淺藍(lán)色,將
UITableView
背景顏色更改為清除。用以下內(nèi)容替換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).") }}
請(qǐng)注意,indexPath.section
使用而不是indexPath.row
為了獲得數(shù)組元素和分接位置的正確值。
你是如何在左右兩側(cè)獲得額外的填充/空間的?
我得到它的方式與為任何視圖添加間距的方式相同。我使用了自動(dòng)布局約束。只需使用Interface Builder中的pin工具為前導(dǎo)和尾隨約束添加間距。

TA貢獻(xiàn)1833條經(jīng)驗(yàn) 獲得超4個(gè)贊
我在單元格之間添加間距的方法是使numberOfSections =“你的數(shù)組計(jì)數(shù)”并使每個(gè)部分只包含一行。然后定義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;}

TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超8個(gè)贊
我使用Swift的簡(jiǎn)單解決方案:
// Inside UITableViewCell subclassoverride func layoutSubviews() { super.layoutSubviews() contentView.frame = contentView.frame.inset(by: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))}
- 3 回答
- 0 關(guān)注
- 2214 瀏覽
添加回答
舉報(bào)