自定義單元格

自定義一個單元格,上面放一個圖片,三個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)//調(diào)整文字大小
        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)//調(diào)整文字大小
        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
        
        //點擊按鈕觸發(fā)CustomCell.onClick函數(shù)
        self.myButton.addTarget(self, action: #selector(CustomCell.onClick), for: UIControlEvents.touchUpInside)


        self.addSubview(self.myButton)
        
    
    }
    
    //定義按鈕的觸發(fā)函數(shù)
    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
        
    }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,257評論 4 61
  • 記錄一個菜鳥的iOS學習之旅,如能幫助正在學習的你,亦楓不勝榮幸;如路過的大神如指教幾句,亦楓感激涕淋! iOS系...
    亦楓閱讀 1,956評論 4 9
  • 街燈是昏黃的光 小草有淡淡的香 路口的豆?jié){冒著熱氣 油條一根根迎風站立 雙手插進口袋 圍巾裹住了臉 耳機在脖子上繞...
    麥子青閱讀 223評論 0 2
  • 文字:若木菡 攝影:若木菡 從去冬就開始盼望春的鐵腳海棠,風兒剛把春的訊息帶到,她就迫不及待地怒放枝頭。 紅梅,年...
    若木菡閱讀 951評論 13 52
  • 潘潘潘咯咯咯閱讀 127評論 0 0

友情鏈接更多精彩內(nèi)容