(承14/30 UI元件之TableViewController-1)
轉場時觸發的事件prepare(for segue:):
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail"{
if let dvc = segue.destination as? DetailViewController{
let selectedIndexPath = self.tableView.indexPathForSelectedRow
//optional biding 所選的列,或是沒有選到列
if let selectedRow = selectedIndexPath?.row{
dvc.infoFromViewOne = animalArray[selectedRow]
dvc.navigationItem.title = animalArray[selectedRow]
if let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? SpecialTableViewCell{
cell.specialLabel.text = animalArray[indexPath.row]
cell.specialImageView.image = UIImage(named: animalArray[indexPath.row])
return cell
如果轉型失敗,就用預設的cell顯示資料:
else{
let cell = UITableViewCell()
cell.textLabel?.text = animalArray[indexPath.row]
cell.imageView?.image = UIImage(named: animalArray[indexPath.row])
return cell
}