Swift之自定义UITableViewCell

前端之家收集整理的这篇文章主要介绍了Swift之自定义UITableViewCell前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

主要是在重写父类init(style:UITableViewCellStyle,reuseIdentifier:String?)中添加要实现的部分,比如下面在cell中添加几个Label,获取改变原有cell中subViews子控件的位置坐标、字体颜色、背景颜色等等。

import UIKit

class HoMetableViewCell: UITableViewCell {

    var addressView:UIView?
    
    var addressLabel:UILabel?
    var distanceLabel:UILabel?
    
    required init?(coder aDecoder:NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(style:UITableViewCellStyle,reuseIdentifier:String?) {
        
        super.init(style: style,reuseIdentifier: reuseIdentifier)
        self.setUpUI();
    }
    
    func setUpUI()
    {
        self.addressView = UIView(frame: CGRect(x: 0,y: 0,width: SCREEN_WIDTH,height: 25))
        self.addressLabel = UILabel(frame: CGRect(x: 0,width: 200,height: 25))
        self.addressLabel?.textColor = UIColor.lightGray
        self.addressLabel?.font = UIFont.systemFont(ofSize: 15)
        self.distanceLabel = UILabel(frame: CGRect(x: 0,width: 100,height: 25))
        self.distanceLabel?.textColor = UIColor.lightGray
        self.distanceLabel?.font = UIFont.systemFont(ofSize: 15)
        self.distanceLabel?.textAlignment = .right
        
        self.addressView?.addSubview(self.addressLabel!)
        self.addressView?.addSubview(self.distanceLabel!)
        
        self.addSubview(self.addressView!)
        
        self.detailTextLabel?.textColor = UIColor.lightGray
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //        let imgHeight:CGFloat = (self.imageView?.frame.size.height)!
        //        let imgyy:CGFloat = (self.bounds.size.height - imgHeight * 0.9)/2
        //        var imgRect = self.imageView?.frame
        //        imgRect?.origin.y = imgyy
        //        imgRect?.size = CGSize(width: imgHeight * 0.9,height: imgHeight * 0.9)
        //        self.imageView?.frame = imgRect!
        //
        //        self.imageView?.layer.borderWidth = 0.3
        //        self.imageView?.layer.borderColor = UIColor.white.cgColor
        //        self.imageView?.layer.cornerRadius = imgHeight/2
        let cellHeight = self.bounds.size.height
        
        //detailTextLabel origin y
        var detailLabelRect = self.detailTextLabel?.frame
        let detailY = (cellHeight - (detailLabelRect?.size.height)!) / 2
        detailLabelRect?.origin.y = detailY
        self.detailTextLabel?.frame = detailLabelRect!
        
        //textLabel rect
        var textRect = self.textLabel?.frame
        textRect?.origin.y = detailY - 5 - (textRect?.size.height)!
        self.textLabel?.frame = textRect!
        
        //addressView rect
        let addrViewX = detailLabelRect?.origin.x
        let addrViewY = (detailLabelRect?.origin.y)! + (detailLabelRect?.size.height)! + 3
        let addrViewWidth = SCREEN_WIDTH - addrViewX!
        
        
        self.addressView?.frame = CGRect(x: addrViewX!,y: addrViewY,width: addrViewWidth,height: 25)
        let disLabelX = (self.addressLabel?.frame.size.width)! - 110
        var disLabelRect = self.distanceLabel?.frame
        
        disLabelRect?.origin.x = disLabelX
        
        self.distanceLabel?.frame = disLabelRect!
        
        
        
    }



    override func setSelected(_ selected: Bool,animated: Bool) {
        super.setSelected(selected,animated: animated)

        // Configure the view for the selected state
    }

}
原文链接:https://www.f2er.com/swift/321603.html

猜你在找的Swift相关文章