[赵三哥Swift学习笔记]UITableView

添加委托

class ViewController:UIViewController,UITableViewDelegate, UITableViewDataSource

定义总行数

    @objc func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return arr.count
    }
显示每一条数据

    @objc func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        let  cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "SimpleTableCell")
        let index = indexPath.row
        let date = arr_logs[index].createdAt
        cell.textLabel!.text = "\(arr[index].name)"
        cell.imageView?.image = UIImage(named: "list")
        cell.detailTextLabel?.text = "\(arr[index].date)"
        return cell;
    }

cell的select事件

    @objc func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("Selected \(indexPath.row)!")
        let index = indexPath.row
        var segue = ""
        switch index
        {
             case 0:segue = "firstSegue"
             case 1:segue = "secondSegue"
             case 2:segue = "thirdSegue"
             default:break
        }
        self.navigationController!.performSegueWithIdentifier(segue, sender: nil)
    }
设置表格名称

    @objc func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "我是标题"
    }
设置分区个数

    @objc func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

自定义表格Cell

import Foundation
import UIKit

class MyCell:UITableViewCell
{
    @IBOutlet weak var lb_name: UILabel!
    @IBOutlet weak var lb_times: UILabel!
}

class ViewController:UIViewController,UITableViewDelegate, UITableViewDataSource
{
    var arr = [“Task1”,”Task2”,”Task3”,”Task4”,”Task5”]
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    // total record count
    @objc func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return arr.count
        
    }
    
    //display each item
    @objc func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        var cell:MyCell = tableView.dequeueReusableCellWithIdentifier(“MyCell", forIndexPath: indexPath) as! MyCell
        if cell.isEqual(nil) {
            cell = MyCell(style: UITableViewCellStyle.Default, reuseIdentifier: “MyCell")
        }
        cell.lb_name.text = "\(arr[indexPath.row])"
        cell.lb_times.text = “100 times"
        return cell; 
    }
}

表格cell的系统样式四种

UITableViewCellStyle.Default

UITableViewCellStyle.Value1

UITableViewCellStyle.Value2

UITableViewCellStyle.Subtitle


设置cell的点击无样式变化

cell.selectionStyle = UITableViewCellSelectionStyle.None

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值