【swift学习笔记】三.使用xib自定义UITableViewCell

简介:

使用xib自定义tableviewCell看一下效果图

1.自定义列

新建一个xib文件 carTblCell,拖放一个UITableViewCell,再拖放一个图片和一个文本框到tableviewcell上

并给我们的xib一个标识

为了学习,我这里的xib和后台的class是分开建的。我们再建一个cocoa touch class文件名称为CarCellTableViewCell继承自UITableViewCell

并把我们的xib和新建的CarCellTableViewCell建立联接

 

在CarCellTableViewCell里建立和xib的图片和文本框的输出

复制代码
import UIKit

class CarCellTableViewCell: UITableViewCell {

    @IBOutlet weak var cellImg: UIImageView!

    @IBOutlet weak var lbCell: UILabel!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        cellImg.layer.borderWidth = 1
        cellImg.layer.masksToBounds = true
        //cellImg.layer.cornerRadius = 31
    }

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

        // Configure the view for the selected state
    }

}
复制代码

 

2.关联cell和tableview

1. 在main.storyboard上拖放一个uitableview,并在后台代码建立输出联接

1.在load事件里注册xib

2.在tableveiw的方法里得到当前的列,指定数据源。

复制代码
import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var tableView: UITableView!
    var tableData: [String] = ["BMW", "Ferrari", "Lambo"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let cellNib = UINib(nibName: "carTblCell", bundle: nil)
        tableView.registerNib(cellNib, forCellReuseIdentifier: "cell")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
         return tableData.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell: CarCellTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! CarCellTableViewCell
        cell.lbCell.text = tableData[indexPath.row]
        cell.cellImg.image = UIImage(named: tableData[indexPath.row])
        
        return cell
    }
    
    func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
        print("\(indexPath.row)")
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 70
    }
}
复制代码

相关文章
|
Swift
Swift -banner滚动图自定义
Swift -banner滚动图自定义
183 0
Swift -banner滚动图自定义
|
Swift
Swift - 自定义tabbar的封装
Swift - 自定义tabbar的封装
286 0
Swift - 自定义tabbar的封装
|
Swift
Swift之自定义Button
Swift之自定义Button
195 0
Swift之自定义Button
|
Swift
Swift5.1—自定义运算符
Swift5.1—自定义运算符
116 0
|
Swift
swift微博第17天(UITableViewCell的自定义)
swift微博第17天(UITableViewCell的自定义)
212 0
|
Swift
swift微博第9天(自定义微博首页的菜单)
swift微博第9天(自定义微博首页的菜单)
154 0
swift微博第9天(自定义微博首页的菜单)
|
Swift
swift微博第8天(自定义标题按钮)
swift微博第8天(自定义标题按钮)
77 0
|
Swift
Swift学习笔记——页面跳转
创建一个single view app后,项目中有main.storyboard,里面是一个viewcontroller。 那么我们如何实现页面跳转 首先添加一个导航控制器Navigation Controller。点击右上的➕,然后选择控件拖到面板上即可
1150 0
|
前端开发 测试技术 Swift
Swift学习笔记——新建项目
在xcode菜单中选择 new -> project -> single view app 点击next在弹出窗中填写项目名称 这里languge有可以选择object-c或swift作为项目语言。这里我们学习swift,所以选择swift。 如果language选择swift,下面的user interface可以选择swiftUI和storyboard。 SwiftUI是2019 年 WWDC 大会上,苹果在压轴环节向大众宣布了基于 Swift 语言构建的全新 UI 框架,与flutter类似,是用代码编写页面,支持快速预览。
664 0
|
Swift iOS开发
UISlider自定义滑动条高度(swift)
UISlider滑动条的高度系统默认给我们设定了,没有办法进行改变。可是,我们的设计又是需要改变这个高度的,怎么办呢?这个时候,我们可以写一个UISlider的子类,重写四个方法就能实现了。
3465 0

相关课程

更多