自定义单元格

自定义一个单元格,上面放一个图片,三个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
        
    }
    }
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 16,955评论 4赞 61
  • 记录一个菜鸟的iOS学习之旅,如能帮助正在学习的你,亦枫不胜荣幸;如路过的大神如指教几句,亦枫感激涕淋! iOS系...
    亦枫阅读 2,006评论 4赞 9
  • 街灯是昏黄的光 小草有淡淡的香 路口的豆浆冒着热气 油条一根根迎风站立 双手插进口袋 围巾裹住了脸 耳机在脖子上绕...
    麦子青阅读 235评论 0赞 2
  • 文字:若木菡 摄影:若木菡 从去冬就开始盼望春的铁脚海棠,风儿刚把春的讯息带到,她就迫不及待地怒放枝头。 红梅,年...
    若木菡阅读 997评论 13赞 52
  • 潘潘潘咯咯咯阅读 129评论 0赞 0

友情链接更多精彩内容