Swift : Customer Cell Tableview trong ios swift.
Bài này mình sẽ hướng dẫn các bạn custom tableViewCell trong code swift.
1. Như bào trước mình đã hướng dẫn các bạn tạo và hiển thị dữ liệu lên tableview
-> tạo và hiển thị dữ liệu trong tableview
-> tạo và hiển thị dữ liệu trong tableview
2. Các bạn tạo cho mình 1 folder tên là customview
Tiếp theo tạo 1 file tên là : tableViewCellExtension.swift chúng ta sẽ viết code lên file này
và có nội dung sau:
import Foundation
import UIKit
class tableViewCellExtension:UITableViewCell {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
}
vì các bạn custom cell nên bắt buộc ta phải kế thừa lại thằng cha nó là UITableViewCell
sao khi tạo xong được như bên trên ta tiến hành tạo giao diện trên cell.
Ở đây mình sẽ làm giao diện gồm có : ảnh icon + tiêu đề , các bạn có thể custom theo các bạn như dưới đây mình hướng dẫn
3.Thực hiện custom
- Khai báo đối tượng imageview -> để hiển thị ảnh
- Khai báo tiếp theo là đối tượng UILabel - > để hiển thị text
private var image_view:UIImageView!
private var txt_title:UILabel!
- Tạo func dưng giao diện trong cell
func settup(){
// tao vi tri cho image trong cell
image_view = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
// tao vi tri label trong cell
txt_title = UILabel(frame: CGRect(x: self.image_view.frame.width + 10, y: 0, width: self.frame.width - (self.image_view.frame.width + 10), height: 60))
// add no vao cell
self.addSubview(image_view)
self.addSubview(txt_title)
}
Và đặt nó trong hàm init :
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
settup()
}
- Tạo một hàm chúng ta sẽ truyền vào cell nội dung : setcontent() -
// tao ham truyen gia tri vao trong cell
func setcontent(name_iamge:String!,txt_title:String!){
self.image_view.image = UIImage(named: name_iamge)
self.txt_title.text = txt_title
}
// setting font size cho text trong cell
func fontSize(){
txt_title.font = UIFont(name: "Arial", size: 18)
txt_title.textColor = UIColor.redColor()
}
-> code đầy đủ sẽ như hình dưới đây
-> chúng ta đã tạo xong cell của table view bây giờ các bạn vào file viewcontronller.swift
-> đến hàm viewDidload()
thêm dòng : để register cell này chó table
self.table_view.registerClass(tableViewCellExtension.self, forCellReuseIdentifier: "cell")
-> vào func :
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {}
và tạo lại như sau:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// khai bao va khoi tao 1 cell
let cell = self.table_view.dequeueReusableCellWithIdentifier("cell") as! tableViewCellExtension
// set content
cell.setcontent("icon", txt_title: "text label")
//sau do chung ta return cell
return cell
}
-> các bạn lấy ở đâu đó file ảnh "icon.png " rồi copy vào project
sau đó click chuột phải vào folder
chọn add files to ... trỏ đến ảnh mà các bạn vừa bỏ vào và add
nếu các bạn ko làm như vậy thì ảnh của các bạn sẽ ko hiển thị ra.
-> code đầy đủ:
--> đến đây thì các bạn đã hoàn thành xong và có thể build project để xem kq nhé
Cám ơn các bạn đã theo dõi.






Comments
Post a Comment