美文网首页
必须实现的一些方法

必须实现的一些方法

作者: endless7 | 来源:发表于2017-11-17 12:36 被阅读0次

只有一组时 通知tableview有几个cell

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

不止一组时 通知tableview每一组有几个cell

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return info[section].count
    }

每个cell要显示的内容

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // 取得 tableView 目前使用的 cell
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell
        
        // 設置 Accessory 按鈕樣式
        if indexPath.section == 1 {
            if indexPath.row == 0 {
                cell.accessoryType = .checkmark
            } else if indexPath.row == 1 {
                cell.accessoryType = .detailButton
            } else if indexPath.row == 2 {
                cell.accessoryType = .detailDisclosureButton
            } else if indexPath.row == 3 {
                cell.accessoryType = .disclosureIndicator
            }
        }
        
        // 顯示的內容
        if let myLabel = cell.textLabel {
            myLabel.text = "\(info[indexPath.section][indexPath.row])"
        }

        return cell
    }

// 點選 cell 後執行的動作
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        // 取消 cell 的選取狀態
        tableView.deselectRow(at: indexPath, animated: true)
        
        let name = info[indexPath.section][indexPath.row]
        print("選擇的是 \(name)")
    }

    // 點選 Accessory 按鈕後執行的動作
    // 必須設置 cell 的 accessoryType
    func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
        let name = info[indexPath.section][indexPath.row]
        print("按下的是 \(name) 的 detail")
    }

    // 有幾組 section
    func numberOfSections(in tableView: UITableView) -> Int {
        return info.count
    }
    
    // 每個 section 的標題
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        let title = section == 0 ? "籃球" : "棒球"
        return title
    }

相关文章

  • OC协议

    协议:Protocol 它可以声明一些必须实现的方法和选择实现的方法 作用:用来声明一些方法 由一些列的方法声明组...

  • 必须实现的一些方法

    只有一组时 通知tableview有几个cell 不止一组时 通知tableview每一组有几个cell 每个ce...

  • iOS -- 通过协议提供匿名对象(16)

    协议定义了一些列方法, 遵从此协议的对象应该事实现他们(如果这些方法不是可选的,那么就必须实现),于是,我...

  • 协议Protocol

    一、协议的基本概念 1、protocol它可以声明一些必须实现的方法和选择实现的方法,这与java是完全不同的 2...

  • 快捷方式

    导入调整,alt+ enter (在继承和实现时必须实现一些方法) 终端查看路径 pwd 查看类结构 cmd +...

  • 第四章 协议与分类—第28条:通过协议提供匿名对象

    协议定义了一些列方法,遵从此协议的对象应该实现它们(如果这些方法不是可选的,那么就必须实现)。于是,我们可以用协议...

  • 用系统方法自定义滑动Cell 视图(兼容iOS7)

    实现效果 实现系统原生的删除方法 必须要实现系统原生的两个方法方法editingStyleForRowAtInde...

  • UITableView

    UITableView的数据源和代理: 数据源方法 必须要实现的数据源方法 (1 ,2 必须实现) 1. - (...

  • 7.8 对象接口

    接口类似抽象类,使用者都必须实现某些方法,抽象类中的普通方法不必实现,而且接口类中只能有公开的普通方法且都必须实现...

  • xcode报错:does not conform to prot

    原因是遵守了协议,但没有完全实现协议中的方法,把必须实现的方法实现即可.

网友评论

      本文标题:必须实现的一些方法

      本文链接:https://www.haomeiwen.com/subject/psxjvxtx.html