Đây là video ghi lại một kỹ năng nhỏ trong buổi học lập trình UITableView của khoá học Lập trình Apple iOS Swift căn bản đến nâng cao.
Mã nguồn ví dụ và nhiều ví dụ nâng cao, bài tập thực hành được chia sẻ tại lớp học. Tuy nhiên, các bạn hoàn toàn có thể code theo video là tạo được.
Một số chú ý khi lập trình customize UITableViewCell
class CustomCell: UITableViewCell {
@IBOutlet weak var thumb: UIImageView!
@IBOutlet weak var title: UILabel!
@IBOutlet weak var score: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Phải đăng ký XIB cho custom UITableViewCell
let nibCell = UINib(nibName: "CustomCell", bundle: nil)
self.tableView.registerNib(nibCell, forCellReuseIdentifier: cellID)
Hàm trả về Custom UITableViewCell
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: CustomCell!
if let dequeCell = tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath) as? CustomCell {
cell = dequeCell
}
let poster = data[indexPath.row]
cell.title.text = poster.title
cell.thumb.image = poster.image
cell.score.text = "(poster.score)"
return cell
}
Class Poster lưu dữ liệu đổ vào custom UITableViewCell
import Foundation
import UIKit
class Poster {
var title: String
var score: Float
private var _image: UIImage
var image: UIImage {
get {
return _image
}
}
init (title: String, imagePath: String, score: Float) {
self.title = title
_image = UIImage(named: imagePath)!
self.score = score
}
}
Bình luận