Ios 如何使tableview部分保持良好状态?
我有两个部分,这些单元格有复选框。这些复选框控制tableview展开移动。在我的代码中,当我单击第0节的复选框时,第0节工作良好,但当我尝试将其设置为与第1节相同的内容时,我出现了错误,因为我无法进行节检测。当我单击第1节复选框时,它将继续关闭第0节。如何在操作函数中访问tableview部分 下面是我的复选框操作功能代码:Ios 如何使tableview部分保持良好状态?,ios,swift,xcode,Ios,Swift,Xcode,我有两个部分,这些单元格有复选框。这些复选框控制tableview展开移动。在我的代码中,当我单击第0节的复选框时,第0节工作良好,但当我尝试将其设置为与第1节相同的内容时,我出现了错误,因为我无法进行节检测。当我单击第1节复选框时,它将继续关闭第0节。如何在操作函数中访问tableview部分 下面是我的复选框操作功能代码: @objc func onValueChanged(sender : M13Checkbox?){ switch sender!.checkState
@objc func onValueChanged(sender : M13Checkbox?){
switch sender!.checkState {
case .checked:
// sender a int vererek bir dene bakalım belki çalışır
let indexPath = IndexPath(row: (sender!.tag), section: 0)
let indexPathTeslim = IndexPath(row: (sender!.tag), section: 1)
let adresso = self.userAdressDict[indexPath.row]
let adressoTeslim = self.userAdressDictForTeslim[indexPathTeslim.row]
//print("\(adressoTeslim.userIlce!)" + " / " + "\(adressoTeslim.userMahalle!)" + " / " + "\(adressoTeslim.userSokak!)" + " / " + "\(adressoTeslim.userApartman!)" + " / No: " + "\(adressoTeslim.userNumara!)" + " / Kat: " + "\(adressoTeslim.userKat!)")
//print("bu alım adresidir: ")
//print("\(adresso.userIlce!)" + " / " + "\(adresso.userMahalle!)" + " / " + "\(adresso.userSokak!)" + " / " + "\(adresso.userApartman!)" + " / No: " + "\(adresso.userNumara!)" + " / Kat: " + "\(adresso.userKat!)")
let adresAlim = ("\(adresso.userMahalle!)" + " / " + "\(adresso.userSokak!)" + " / " + "\(adresso.userApartman!)")
secilenAdresForAlim = adresAlim
let deadlineTime = DispatchTime.now() + .milliseconds(500)
DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
if indexPath.section == 0 {
if self.keys[0] == 0 {
self.keys[0] = 1
}else{
self.keys[0] = 0
self.buttonLabel = "Değiştir"
self.headerTitleForSecZero = self.secilenAdresForAlim
}
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
if indexPathTeslim.section == 0 {
if self.keysTeslim[0] == 0 {
self.keysTeslim[0] = 1
}else{
self.keysTeslim[0] = 0
}
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
break
case .unchecked:
print("adres kaldırıldı bile...")
break
case .mixed:
//empty...
break
}
}
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "AdressCell", for: indexPath) as! AdressCell
let adresGelen = self.userAdressDict[indexPath.item]
cell.adresLabel.text = "\(adresGelen.userIlce!)" + " / " + "\(adresGelen.userMahalle!)" + " / " + "\(adresGelen.userSokak!)" + " / " + "\(adresGelen.userApartman!)" + " / No: " + "\(adresGelen.userNumara!)" + " / Kat: " + "\(adresGelen.userKat!)"
cell.checkBox.tag = indexPath.row
//cell.checkBox.addTarget(self, action: #selector(siparisOnayiController.onValueChanged(sender:)), for: UIControl.Event.valueChanged)
cell.checkBox.addTarget(self, action: #selector(siparisOnayiController.onValueChanged(sender:)), for: UIControl.Event.valueChanged)
return cell
}
else {
let cell = tableView.dequeueReusableCell(withIdentifier: "teslimAdressCell", for: indexPath) as! teslimAdressCell
let adresGelen = self.userAdressDictForTeslim[indexPath.item]
cell.adresLabel.text = "\(adresGelen.userIlce!)" + " / " + "\(adresGelen.userMahalle!)" + " / " + "\(adresGelen.userSokak!)" + " / " + "\(adresGelen.userApartman!)" + " / No: " + "\(adresGelen.userNumara!)" + " / Kat: " + "\(adresGelen.userKat!)"
cell.checkBox.tag = indexPath.row
cell.checkBox.addTarget(self, action: #selector(siparisOnayiController.onValueChanged(sender:)), for: UIControl.Event.valueChanged)
return cell
}
}
下面是我的cellForRow函数代码:
@objc func onValueChanged(sender : M13Checkbox?){
switch sender!.checkState {
case .checked:
// sender a int vererek bir dene bakalım belki çalışır
let indexPath = IndexPath(row: (sender!.tag), section: 0)
let indexPathTeslim = IndexPath(row: (sender!.tag), section: 1)
let adresso = self.userAdressDict[indexPath.row]
let adressoTeslim = self.userAdressDictForTeslim[indexPathTeslim.row]
//print("\(adressoTeslim.userIlce!)" + " / " + "\(adressoTeslim.userMahalle!)" + " / " + "\(adressoTeslim.userSokak!)" + " / " + "\(adressoTeslim.userApartman!)" + " / No: " + "\(adressoTeslim.userNumara!)" + " / Kat: " + "\(adressoTeslim.userKat!)")
//print("bu alım adresidir: ")
//print("\(adresso.userIlce!)" + " / " + "\(adresso.userMahalle!)" + " / " + "\(adresso.userSokak!)" + " / " + "\(adresso.userApartman!)" + " / No: " + "\(adresso.userNumara!)" + " / Kat: " + "\(adresso.userKat!)")
let adresAlim = ("\(adresso.userMahalle!)" + " / " + "\(adresso.userSokak!)" + " / " + "\(adresso.userApartman!)")
secilenAdresForAlim = adresAlim
let deadlineTime = DispatchTime.now() + .milliseconds(500)
DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
if indexPath.section == 0 {
if self.keys[0] == 0 {
self.keys[0] = 1
}else{
self.keys[0] = 0
self.buttonLabel = "Değiştir"
self.headerTitleForSecZero = self.secilenAdresForAlim
}
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
if indexPathTeslim.section == 0 {
if self.keysTeslim[0] == 0 {
self.keysTeslim[0] = 1
}else{
self.keysTeslim[0] = 0
}
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
break
case .unchecked:
print("adres kaldırıldı bile...")
break
case .mixed:
//empty...
break
}
}
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "AdressCell", for: indexPath) as! AdressCell
let adresGelen = self.userAdressDict[indexPath.item]
cell.adresLabel.text = "\(adresGelen.userIlce!)" + " / " + "\(adresGelen.userMahalle!)" + " / " + "\(adresGelen.userSokak!)" + " / " + "\(adresGelen.userApartman!)" + " / No: " + "\(adresGelen.userNumara!)" + " / Kat: " + "\(adresGelen.userKat!)"
cell.checkBox.tag = indexPath.row
//cell.checkBox.addTarget(self, action: #selector(siparisOnayiController.onValueChanged(sender:)), for: UIControl.Event.valueChanged)
cell.checkBox.addTarget(self, action: #selector(siparisOnayiController.onValueChanged(sender:)), for: UIControl.Event.valueChanged)
return cell
}
else {
let cell = tableView.dequeueReusableCell(withIdentifier: "teslimAdressCell", for: indexPath) as! teslimAdressCell
let adresGelen = self.userAdressDictForTeslim[indexPath.item]
cell.adresLabel.text = "\(adresGelen.userIlce!)" + " / " + "\(adresGelen.userMahalle!)" + " / " + "\(adresGelen.userSokak!)" + " / " + "\(adresGelen.userApartman!)" + " / No: " + "\(adresGelen.userNumara!)" + " / Kat: " + "\(adresGelen.userKat!)"
cell.checkBox.tag = indexPath.row
cell.checkBox.addTarget(self, action: #selector(siparisOnayiController.onValueChanged(sender:)), for: UIControl.Event.valueChanged)
return cell
}
}
按钮标签只允许您传递一个
Int
,这对于同时需要行和节的人来说是不够的(比如您的案例)
在单元格的类中定义一个闭包:
var action : (() -> Void)? = nil
然后为单元格类中的复选框按钮(或任何按钮)创建操作函数:
@IBAction func btnAction(sender: M13Checkbox)
{
switch sender!.checkState {
case .checked:
if let act = self.action
{
act()
}
break
case .unchecked:
print("adres kaldırıldı bile...")
break
case .mixed:
//empty...
break
}
}
然后在cellForRow
func中传递闭包:
cell.action = {
//here you have access to indexPath.section and indexPath.row
let adresso = self.userAdressDict[indexPath.row] // teslimde calismaz suanda..
print("\(adresso.userIlce!)" + " / " + "\(adresso.userMahalle!)" + " / " + "\(adresso.userSokak!)" + " / " + "\(adresso.userApartman!)" + " / No: " + "\(adresso.userNumara!)" + " / Kat: " + "\(adresso.userKat!)")
let adresAlim = ("\(adresso.userMahalle!)" + " / " + "\(adresso.userSokak!)" + " / " + "\(adresso.userApartman!)")
secilenAdresForAlim = adresAlim
let deadlineTime = DispatchTime.now() + .milliseconds(500)
DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
if self.keys[0] == 0 {
self.keys[0] = 1
}else{
self.keys[0] = 0
self.buttonLabel = "Değiştir"
self.headerTitleForSecZero = self.secilenAdresForAlim
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
这比按标签确定单元格更合适。现在,当您传递闭包时,您可以访问闭包中的indepath.row
和indepath.section
。在操作函数中,您从sender.tag
创建一个indepath,然后通过indepath.row
访问数据源索引。为什么不先通过sender.tag
访问它呢?比如:self.useraddressdict[sender.tag]
@Soroush据我所知,你的意思是它们不会干扰,因为有不同的字典。没错,但我如何为其发送者分配身份?@swifty2:您在tableView上遇到的所有这些问题,包括崩溃和“不工作”的问题,让我觉得您的数据模型有缺陷,您不太了解数据源是如何工作的。请看这里的例子:@Koen我当然会看到。谢谢你的回答,但我有一些问题,当我试图调用我的自定义单元格类我的按钮操作有很多错误,因为数据。。。有什么不同的解决方法吗?您是在viewController类还是cell的类中分配单元格。操作
?是的,我在我的单元格类中分配。您不应该!正如我在回答中所写,您应该在cellForRow
函数中分配它。这样,由于您的数据在类中可用,您将不会得到任何错误。@swifty2