swift -> 纯代码实现 tableview

** 点击某行 返回 对应 行的 Cell

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let cl = tableView.cellForRowAtIndexPath(indexPath);
        cl?.frame.origin.y;
        print(cl?.frame.origin.y)
        //clickMes(CGFloat(indexPath.row));
        //修改cell中的某一个元素
        (tableView.cellForRow(at: indexPath) as! DiyViewCell).title.textColor = UIColor.black
}
    

** 设置 每行高度

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

         return 60;

}

** 设置分割线位移

    //设置分割线位移
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        cell.separatorInset = UIEdgeInsets(top: 0, left: 50, bottom: 0, right: 0);
    }

** 判断某个indexPath 是否显示在当前的tablve 的可视范围内,即是否有效, 只有有效才能操作对应的cell

if(tableView.indexPathsForVisibleRows?.contains(indexPath))!{

}
 
扫描二维码关注公众号,回复: 281201 查看本文章

============================== ==========================================

  1,  自定义cell --------------------------------------------------

class Tb: UITableViewController {
    var baby = ["数据0","数据1","数据2","数据3","数据4","数据5","数据6","数据7","数据8","数据9","数据10","数据11"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source
    //每行有几个区块
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }
    //多少行
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return baby.count
    }

    // 开始往每行写内容
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //---
        if(indexPath.row < 5 ){//如果indexPath < 5 就使用默认的cell系统样式
            let cell = UITableViewCell.init(style: UITableViewCellStyle.Default, reuseIdentifier: "diy_cell");
            cell.textLabel?.text = baby[indexPath.row];
            return cell;
        }else{// 如果indexPath >=5 就使用 自定义的 DiyViewCell 样式
            let cell = DiyViewCell.init(style: UITableViewCellStyle.Default, reuseIdentifier: "diy_cell") as DiyViewCell;
            cell.title.text = baby[indexPath.row];
            cell.clickBtn.setTitle("["+baby[indexPath.row]+"]", forState: UIControlState.Normal);
            return cell;
        }
        
        //====
    }
    

}

class DiyViewCell: UITableViewCell {
    var title:UILabel!
    var clickBtn:UIButton!
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        if self.isEqual(nil){return;}
        ///
        title = UILabel(frame: CGRectMake(20, 20, 200, 30));
        title.textColor = UIColor.redColor();
        self.contentView.addSubview(title)
        clickBtn = UIButton(frame: CGRectMake(200, 20, 60, 30))
        //clickBtn.setTitle("app", forState: UIControlState.Normal)
        clickBtn.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
        clickBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Highlighted)
        self.contentView.addSubview(clickBtn)
       
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
}

结果



 

 -----------------------------------------------------------------------------------------------------------------

2 , 先是使用系统默认样式cell 的-----

class Tb: UITableViewController {
    var baby = ["数据0","数据1","数据2","数据3","数据4","数据5","数据6","数据7","数据8","数据9","数据10","数据11"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source
    //每行有几个区块
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }
    //多少行
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return baby.count
    }

    // 开始往每行写内容
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        var cell = tableView.dequeueReusableCellWithIdentifier("diy_cell");
        
        if(cell == nil){//因为是纯代码实现,没有对行里的cell做注册,这里是 做注册, 注册一次后,下次会继续使用这个缓存
            cell = UITableViewCell.init(style: UITableViewCellStyle.Default, reuseIdentifier: "diy_cell");
            //以上使用了系统默认的一个cell样式
        }
        cell?.textLabel?.text = baby[indexPath.row];
        return cell!
    }

 结果:


猜你喜欢

转载自mft.iteye.com/blog/2314641