Swift TableViewController中的多个自定义单元格
情景Swift TableViewController中的多个自定义单元格,swift,uitableview,Swift,Uitableview,情景 |第一单元| |第二单元| |开关| || |第三单元| |文本字段| 我用三个不同的类创建了一个动态表视图:SecondTableCell(内部有一个开关的出口)和ThirdTableCell(内部有一个文本字段的出口) 开关关了 我需要查看textField.isUserInteractionEnabled=false,然后开关打开 我该怎么做 class ViewController: UIViewController, UITableViewDelegate, UITa
|第一单元|
|第二单元| |开关| ||
|第三单元| |文本字段|
我用三个不同的类创建了一个动态表视图:
SecondTableCell
(内部有一个开关的出口)和ThirdTableCell
(内部有一个文本字段的出口)
开关关了
我需要查看textField.isUserInteractionEnabled=false
,然后开关打开
我该怎么做
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var customTableView: UITableView!
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellSwitch = tableView.dequeueReusableCell(withIdentifier: "cellSwitch")! as! SwitchTableViewCell
let cellText = tableView.dequeueReusableCell(withIdentifier: "cellText")! as! TextFieldTableViewCell
}
class SwitchTableViewCell: UITableViewCell {
@IBOutlet weak var switchOutlet: UISwitch!
@IBAction func switchAction(_ sender: UISwitch) {
if sender.isOn == true{
print("swithOn")
}else{
print("swithOff")
}
}
}
class TextFieldTableViewCell: UITableViewCell {
@IBOutlet weak var textFieldColor: UITextField!
}
在tableviewcontroller中设置单元格时,可以在视图行定义要使用哪个tableviewcell类。就像我下面的例子:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if ((indexPath.row < 25 && counter > 25) || (indexPath.row < counter)) {
let cell = tableView.dequeueReusableCell(withIdentifier: "SaleItems", for: indexPath) as! MyTableCellTableViewCell
let TapRecognize = UITapGestureRecognizer(target: self, action: #selector(self.PictureTap(sender:)))
TapRecognize.numberOfTapsRequired = 1
TapRecognize.name = jSonData[indexPath.row]["advid"]
// Configure the cell...
cell.Megnevezes.text=jSonData[indexPath.row]["advdescription"]!
cell.EladasiAr.text=jSonData[indexPath.row]["advprice"]! + " " + jSonData[indexPath.row]["advpricecur"]!
cell.addGestureRecognizer(TapRecognize)
cell.LoadPicture(AdvID: Int(jSonData[indexPath.row]["advid"]!)!)
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "PagerCell", for: indexPath) as! ChangePageCell
cell.delegate = self
return cell
}
}
override func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
if((indexPath.row<25&&counter>25)| |(indexPath.row
在我的示例中,tableview的最后一行是不同的。从你的解释来看,你并不清楚你到底想用开关电池做什么。但是,您应该确保能够识别单元格及其类,以知道在哪里传递操作
在我的例子中,手势识别器获取一个名称,该名称包含一个相关的广告ID以加载照片,并链接到该广告详细信息
当您点击单元格时,它会调用点击识别器的操作,并且可以使用名称来识别触摸了哪个单元格。您的整个tableView中是否只有两个单元格?在这种情况下,您不需要tableview,只需要一个文本字段和一个开关就可以了。我需要在一个动态单元格中设置开关,在另一个动态单元格中设置文本字段。是的,用于练习。您可以发布一些代码吗?我的回答假设您已经有了一个在编译时显示的视图,但我不确定。在CellForRow内部,我已经让cellSwitch=tableView.dequeueReusableCell(带有标识符:“cellSwitch”)!作为!SwitchTableViewCell我尝试实现这一点,但在extendIt中遇到了一些问题。如果您能说出是哪个问题,而不仅仅是说您遇到了问题,这会很有帮助。请编辑您的原始问题并发布您的代码。这是您需要修改的代码的重要部分。有2个错误,类型为“SwitchTableViewCell”的第一个值没有成员“delegate”,类型为“TextFieldTableViewCell”的值没有成员“delegate”,第二个是使用未解析的标识符“cell”工作,但我需要最后一步,因为cell.textField.isUserInteractionEnabled=false执行运行时错误,线程1:致命错误:在展开可选值时意外发现nil,但我没有添加cellText.delegate=self,因为没有
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if ((indexPath.row < 25 && counter > 25) || (indexPath.row < counter)) {
let cell = tableView.dequeueReusableCell(withIdentifier: "SaleItems", for: indexPath) as! MyTableCellTableViewCell
let TapRecognize = UITapGestureRecognizer(target: self, action: #selector(self.PictureTap(sender:)))
TapRecognize.numberOfTapsRequired = 1
TapRecognize.name = jSonData[indexPath.row]["advid"]
// Configure the cell...
cell.Megnevezes.text=jSonData[indexPath.row]["advdescription"]!
cell.EladasiAr.text=jSonData[indexPath.row]["advprice"]! + " " + jSonData[indexPath.row]["advpricecur"]!
cell.addGestureRecognizer(TapRecognize)
cell.LoadPicture(AdvID: Int(jSonData[indexPath.row]["advid"]!)!)
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "PagerCell", for: indexPath) as! ChangePageCell
cell.delegate = self
return cell
}
}