Próbuję zmienić wygląd niestandardowego wybranego TableViewCell za pomocą Swift.Niestandardowy interfejs użytkownika TableViewCell wybrany kolor tła swift
Czy muszę to zrobić przez projektanta lub programowo?
Próbowałem następujące:
I tu jest mój kod:
@IBOutlet var tableView: UITableView!
var tableData: [String] = ["One", "Two", "Three", "Four"]
override func viewDidLoad() {
super.viewDidLoad()
// Register custom cell
var nib = UINib(nibName: "vwTblCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "cell")
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.tableData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as TblCell
cell.lblCarName.text = tableData[indexPath.row]
cell.imgCarName.image = UIImage(named: tableData[indexPath.row])
return cell
}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
println("Row \(indexPath.row) selected")
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 70
}
Dzięki :) to jest we właściwym kierunku. Mam jednak pewien problem: kiedy klikam komórkę, staje się ona oryginalnym szarym kolorem wyboru, kiedy kliknę następną komórkę, poprzednio zaznaczona komórka staje się czerwona. Ale aktualnie wybrana komórka jest zawsze szara. –
Czy wypróbowałeś pierwszy pomysł? – Christian
Próbowałem obu, zachowanie było takie samo dla każdej metody. –