自定义单元格

本文介绍如何创建自定义的UITableViewCell,包括布局一个图片和三个文本标签,并实现了一个覆盖整个单元格的透明按钮及其点击事件处理。

自定义一个单元格,上面放一个图片,三个label,还有一个透明的覆盖单元格的按钮.


定义一个父类为UITableViewCell的类: CustomCell.swift

import UIKit

class CustomCell: UITableViewCell {
    
    var myLabel1: UILabel!
    var myLabel2: UILabel!
    var myLabel3: UILabel!
    var myButton: UIButton!
    var myImageView: UIImageView!
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        
        let viewBounds:CGRect = UIScreen.main.applicationFrame
        
        //单元格的宽度,取屏幕宽度
        let cellWidth: CGFloat = viewBounds.width

        /// 1.添加ImageView,靠在最左边
        self.myImageView = UIImageView(frame: CGRect(x: 0 ,  y: 0, width: 80, height: 80))

        self.addSubview(self.myImageView)
        
        /// 2.添加标签1
        self.myLabel1 = UILabel(frame: CGRect(x: 80, y: 0, width: cellWidth - 80, height: 50))
        self.myLabel1.numberOfLines = 2
        self.addSubview(self.myLabel1)
        
    
        
        /// 3.添加标签2
        self.myLabel2 = UILabel(frame: CGRect(x: 80, y: 50, width: 50, height: 25))
        self.myLabel2.numberOfLines = 1
        self.myLabel2.font=UIFont.systemFont(ofSize: 12)//调整文字大小
        self.addSubview(self.myLabel2)
        
        /// 4.添加标签3 ,靠右边放
        self.myLabel3 = UILabel(frame: CGRect(x: cellWidth - 150, y:50 , width: 150, height: 25))
        self.myLabel3.numberOfLines = 1
        self.myLabel3.textColor = UIColor.red
        self.myLabel3.font=UIFont.systemFont(ofSize: 15)//调整文字大小
        self.addSubview(self.myLabel3)

        /// 5.添加按钮,覆盖整个单元格,透明处理
        self.myButton = UIButton(frame: CGRect(x: 0, y: 0, width: cellWidth, height: 80))
     //   self.myButton.setTitleColor(UIColor.white, for: UIControlState())
        //self.myButton.backgroundColor=UIColor.red
      //  self.myButton.titleLabel?.numberOfLines = 2
        
        //点击按钮触发CustomCell.onClick函数
        self.myButton.addTarget(self, action: #selector(CustomCell.onClick), for: UIControlEvents.touchUpInside)


        self.addSubview(self.myButton)
        
    
    }
    
    //定义按钮的触发函数
    func onClick(){
        let dizhi = YangSTableViewController.dizhi  //从其他类引入
        UIApplication.shared.openURL(NSURL(string: dizhi) as! URL)
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

YangSTableViewController类:

class YangShiTableViewController: UITableViewController {

    static var dizhi  = "https://xxxxxx"

 let cellIdentifier = "Cell1shi"

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        let cell:CustomCell! = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for:indexPath) as? CustomCell
        
        let row = indexPath.row
        let rowDict = self.listTeams[row] as! NSDictionary
        cell.myLabel1.text = rowDict["name"] as? String
        cell.myLabel2.text = "222"
 
        cell.myLabel3.text = "333"

        YangShiTableViewController.dizhi = url

        
        let urlStr = NSURL(string: (image_url))
        
        let data = NSData(contentsOf: urlStr! as URL)
        cell.myImageView?.image  = UIImage(data: data! as Data)

        
        cell.accessoryType = .none
        
        return cell
        
    }
    }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值