美文网首页
自定义单元格

自定义单元格

作者: yytester | 来源:发表于2016-12-27 16:29 被阅读12次

自定义一个单元格,上面放一个图片,三个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
        
    }
    }

相关文章

网友评论

      本文标题:自定义单元格

      本文链接:https://www.haomeiwen.com/subject/mzhevttx.html