你不能直接连接任何嵌入在 TableCell 中的东西的插座
按照步骤在连接插座的情况下执行代码操作
步骤 1- 创建一个新的 tableViewCell 类,如下面的 ScreenSHot
步骤 2- 现在将创建的类分配给 Storyboard 中的 TableView 单元格,如下所示
第 3 步- 连接单元类中的插座的时间是通过在单元类中正常拖动要连接的 TF 来创建的
输出如下
步骤 4- 所需编码
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomTableCell
cell.fileNameLabel.text = coreClassObject.audioFileName
cell.durationLabel.text = coreClassObject.audioDuration
cell.uploadedStatusLabel.text = coreClassObject.audioUploadStatus
cell.playButton.addTarget(self, action: #selector(playSoundAtSelectedIndex(sender:)), for: .touchUpInside)
return cell
}
在 ViewDidLoad 中重新更新访问 TF 的答案
---->我的 ViewController 类
import UIKit
class tableViewVC: UIViewController
{
let staticArrayy = ["1","1","1","1","1","1","1","1","1","1"]
@IBOutlet weak var myDemoTableView: UITableView!
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view.
///Set Delegates
myDemoTableView.delegate = self
myDemoTableView.dataSource = self
///Async operation
///To make sure cells are loaded
DispatchQueue.main.async
{
///Create a Reference TF
let MyTf : UITextField!
///Get Index Path of Which TF is to be Accessed
let indexPath = IndexPath(row: 0, section: 0)
///Create A new cell Reference
let newCell = self.myDemoTableView.cellForRow(at: indexPath)! as! CustomTableViewCell
///Assign Cell TF to our created TF
MyTf = newCell.cellTF
///Perform Changes
MyTf.text = "Changes text"
}
}
}
extension tableViewVC : UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return staticArrayy.count
}
//Setting cells data
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = self.myDemoTableView.dequeueReusableCell(withIdentifier: "Cell") as! CustomTableViewCell
cell.cellTF.placeholder = staticArrayy[indexPath.row]
return cell
}
//Setting height of cells
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return 60
}
}
--->我的细胞类
import UIKit
class CustomTableViewCell: UITableViewCell
{
@IBOutlet weak var cellTF: UITextField!
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
}
}
---->我的故事板
--->模拟器输出