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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Swift的UITableView示例

Swift的UITableView示例

絕地無雙 2019-07-31 17:56:07
Swift的UITableView示例我已經(jīng)和Swift和iOS合作了好幾個月了。我熟悉許多方法,但我不夠好,我可以不用看就寫出來。我很欣賞Stack Overflow過去提供的快速答案,讓我回到正軌,我已經(jīng)生銹了(例如,AsyncTask Android示例)。iOS UITableView對我來說屬于這一類。我已經(jīng)完成了幾次,但我忘記了細節(jié)。我在StackOverflow上找不到另一個問題,只是要求一個基本的例子,我正在尋找比許多在線教程更短的東西(雖然這個非常好)。我將在下面提供一個答案供我將來參考和你的。
查看完整描述

3 回答

?
隔江千里

TA貢獻1906條經(jīng)驗 獲得超10個贊

以下示例是對We?Swift 的較長帖子的改編和簡化。這就是它的樣子:

創(chuàng)建一個新項目

它可以只是通常的單視圖應用程序。

添加代碼

用以下內(nèi)容替換ViewController.swift代碼:

import UIKitclass ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    // Data model: These strings will be the data for the table view cells    let animals: [String] = ["Horse", "Cow", "Camel", "Sheep", "Goat"]

    // cell reuse id (cells that scroll out of view can be reused)    let cellReuseIdentifier = "cell"

    // don't forget to hook this up from the storyboard    @IBOutlet var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Register the table view cell class and its reuse id        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)

        // (optional) include this line if you want to remove the extra empty cell divider lines        // self.tableView.tableFooterView = UIView()
        // This view controller itself will provide the delegate methods and row data for the table view.        tableView.delegate = self
        tableView.dataSource = self
    }

    // number of rows in table view    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.animals.count    }

    // create a cell for each table view row    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        // create a new cell if needed or reuse an old one        let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!

        // set the text from the data model        cell.textLabel?.text = self.animals[indexPath.row]

        return cell    }

    // method to run when table view cell is tapped    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("You tapped cell number \(indexPath.row).")
    }}

閱讀代碼內(nèi)注釋以了解正在發(fā)生的事情。亮點是

  • 視圖控制器采用UITableViewDelegate和 UITableViewDataSource協(xié)議。

  • numberOfRowsInSection方法確定表視圖中將有多少行。

  • cellForRowAtIndexPath方法設(shè)置每一行。

  • didSelectRowAtIndexPath每次輕敲一行時都會調(diào)用該方法。

將表視圖添加到故事板

將a UITableView拖到View Controller上。使用自動布局固定四個邊。

連接奧特萊斯

Control從IB中的表視圖拖動到tableView代碼中的插座。

成品

就這樣。您應該可以立即運行您的應用程序。

這個答案是用Xcode 9和Swift 4測試的


變化

行刪除

如果要允許用戶刪除行,則只需向上面的基本項目添加單個方法。請參閱此基本示例以了解具體方法。

行間距

如果您希望行之間有間距,請參閱此補充示例

定制單元格

表視圖單元格的默認布局可能不是您所需要的。查看此示例以幫助您開始制作自己的自定義單元格。

動態(tài)細胞高度

有時您不希望每個單元格都具有相同的高度。從iOS 8開始,可以根據(jù)單元格內(nèi)容自動設(shè)置高度。有關(guān)開始使用所需的一切,請參閱此示例

進一步閱讀


查看完整回答
反對 回復 2019-07-31
?
翻過高山走不出你

TA貢獻1875條經(jīng)驗 獲得超3個贊

為了完整起見,以及那些不希望使用Interface Builder的人,這里有一種方法可以完全以編程方式創(chuàng)建與Suragch的答案相同的表- 盡管具有不同的大小和位置。

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource  {

    var tableView: UITableView = UITableView()
    let animals = ["Horse", "Cow", "Camel", "Sheep", "Goat"]
    let cellReuseIdentifier = "cell"

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.frame = CGRectMake(0, 50, 320, 200)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)

        self.view.addSubview(tableView)
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return animals.count    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellReuseIdentifier) as UITableViewCell!

        cell.textLabel?.text = animals[indexPath.row]

        return cell    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("You tapped cell number \(indexPath.row).")
    }}

確保你記得import UIKit


查看完整回答
反對 回復 2019-07-31
  • 3 回答
  • 0 關(guān)注
  • 752 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

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