第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在UITableViewCell之間添加間距

如何在UITableViewCell之間添加間距

陪伴而非守候 2019-08-15 16:32:04
如何在UITableViewCell之間添加間距有沒(méi)有辦法增加間距UITableViewCell?我創(chuàng)建了一個(gè)表,每個(gè)單元格只包含一個(gè)圖像。圖像被分配給單元格,如下所示:cell.imageView.image = [myImages objectAtIndex:indexPath.row];但這會(huì)使圖像放大并適合整個(gè)單元格,圖像之間沒(méi)有間距。或者以這種方式說(shuō),圖像的高度例如是50,并且我想在圖像之間添加20個(gè)間隔。有沒(méi)有辦法實(shí)現(xiàn)這個(gè)目標(biāo)?
查看完整描述

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)和尾隨約束添加間距。


查看完整回答
反對(duì) 回復(fù) 2019-08-15
?
瀟瀟雨雨

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


查看完整回答
反對(duì) 回復(fù) 2019-08-15
?
ITMISS

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


查看完整回答
反對(duì) 回復(fù) 2019-08-15
  • 3 回答
  • 0 關(guān)注
  • 2214 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)