TableView সেলের ব্যাকগ্রাউন্ড রঙ কাস্টমাইজ করুন
টেবল ভিউয়ের পুরো ব্যাকগ্রাউন্ড রঙ পরিবর্তন করা এবং প্রতিটি সেলের (cell) ব্যাকগ্রাউন্ড রঙ পরিবর্তন করা—এই দুটি আলাদা বিষয়। নতুন ডেভেলপাররা প্রায়শই এই দুটো গুলিয়ে ফেলেন। এই পোস্টে শুধু সেলের রঙ পরিবর্তন করার পদ্ধতি দেখানো হলো।
মূল কোড
cellForRowAt মেথডে সেলের contentView.backgroundColor প্রপার্টি সেট করুন:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
cell.contentView.backgroundColor = UIColor.cyan
return cell
}
সম্পূর্ণ বাস্তবায়ন
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.contentView.backgroundColor = UIColor.cyan
cell.textLabel?.text = "Row \(indexPath.row)"
return cell
}
}
নোট: পুরো টেবল ভিউয়ের রঙ পরিবর্তন করতে চাইলে tableView.backgroundColor ব্যবহার করুন।