swift -> TableView 创建局部显示/自定义容器 的 TableView

 

 代码创建TableView 参考 : http://mft.iteye.com/blog/2314641

 

 

创建局部显示 或 自定义 容器 的 TableView

import UIKit


class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    let screenWidth = UIScreen.main.bounds.width;
    let screenHeight = UIScreen.main.bounds.height;
    //
    
    var myTable:UITableView!;
    
    var baby = ["数据0","数据1","数据2","数据3","数据4","数据5","数据6","数据7","数据8","数据9","数据10","数据11"];
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
  
        
        myTable = UITableView(frame: CGRect(x: 10, y: 80, width: 100, height: 300));
        myTable.delegate = self
        myTable.dataSource = self;
        myTable.layer.masksToBounds = true;
        myTable.layer.borderWidth  = 2;
        myTable.layer.borderColor = UIColor.red.cgColor;
        self.view.addSubview(myTable);
        
        
     
        
    }
    

    //每行有几个区块
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }
    //多少行
    func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return baby.count
    }
    
    // 开始往每行写内容
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //---
        var cell = tableView.dequeueReusableCell(withIdentifier: "diy_cell");
        
        if(cell == nil){//因为是纯代码实现,没有对行里的cell做注册,这里是 做注册, 注册一次后,下次会继续使用这个缓存
            cell = UITableViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: "diy_cell");
            //以上使用了系统默认的一个cell样式
        }
        cell?.textLabel?.text = baby[indexPath.row];
        return cell!
        //====
    }
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    
}

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值