我没有使用代码remove from superView
来删除屏幕上的标签,如果没有可用的地址,只显示一个标签,如下所示
但是当我从api获得地址并且没有正确显示并且代码本身进入它时,它显示在图像中,如下所示
但是应该显示带有名称标签,地址标签和手机号码标签的正确图像,任何人都可以帮助我在从api获取地址后从视图中删除后如何显示地址标签和手机号码标签?
这是我的代码
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if (indexPath.section == 0) {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! AddressTableViewCell
let dict = guestShippingAddressModel
self.tableDetails.isHidden = false
self.activityIndicator.stopAnimating()
self.activityIndicator.hidesWhenStopped = true
cell.deleteButton.tag = indexPath.row
if self.street?.isEmpty == true || self.street?.isEmpty == nil {
cell.addressLabel.isHidden = true
cell.mobileNumberLabel.isHidden = true
cell.radioButton.isHidden = true
cell.editButton.isHidden = true
cell.deleteButton.isHidden = true
cell.addresslabel.removeFromSuperview()
cell.mobileNumberlabel.removeFromSuperview()
cell.nameLabel.text = "No address available"
if delayCheck == true {
let when = DispatchTime.now() + 5 // change 2 to desired number of seconds
DispatchQueue.main.asyncAfter(deadline: when) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let addtoCartVC = storyboard.instantiateViewController(withIdentifier: "newAddress") as! NewAddressViewController
self.navigationController?.pushViewController(addtoCartVC, animated: true)
}
}
}
else {
cell.addressLabel.isHidden = false
cell.radioButton.isHidden = false
cell.editButton.isHidden = false
cell.deleteButton.isHidden = false
cell.nameLabel.isHidden = false
cell.nameLabel.text = "\((dict?.firstName)!) \((dict?.lastName)!)"
cell.addressLabel.text = "\((self.street)!) \((dict?.city)!) \((dict?.region)!) \((dict?.postCode)!)"
cell.mobileNumberLabel.text = "\((dict?.telephone)!)"
}
cell.radioButton.tag = indexPath.row
cell.editButton.tag = indexPath.row
cell.deleteButton.tag = indexPath.row
cell.editButton.isHidden = true
cell.deleteButton.isHidden = true
cell.radioButton.addTarget(self, action: #selector(selectRadioButton(_:)), for: .touchUpInside)
cell.deleteButton.addTarget(self, action: #selector(deleteAction(button:)), for: .touchUpInside)
cell.editButton.addTarget(self, action: #selector(editButtonAction(_:)), for: .touchUpInside)
let checkIndex = self.checkIsRadioSelect.index(of: indexPath.row)
if(checkIndex != nil) {
cell.radioButton.isSelected = true
cell.editButton.isHidden = false
cell.deleteButton.isHidden = false
}
else
{
cell.radioButton.isSelected = false
cell.editButton.isHidden = true
cell.deleteButton.isHidden = true
}
if (checkIsPaymentRadioSelect == true) {
let defaultvalue = street
if defaultvalue?.isEmpty == false {
cell.radioButton.isSelected = true
cell.editButton.isHidden = false
cell.deleteButton.isHidden = false
addressSelected = true
}
}
return cell
}
答案 0 :(得分:0)
而不是从superview中删除地址标签和移动标签设置该标签的高度约束,并将高度常量更改为0以隐藏标签并使单元格高度动态如下所示。
tblList.rowHeight = UITableViewAutomaticDimension;
tblList.estimatedRowHeight = CGFloat(100)
答案 1 :(得分:0)
男人,不要在UITableViewDataSource
的dataSource方法中编写这么多代码。处理和理解真正发生的事情非常困难!有太多奇怪的逻辑,我真的不懂。所以我建议你
UITableViewCell
子类(AddressTableViewCell
& NewAddressViewController
中进行所有配置)activityIndicator
不应该以{{1}}方式处理。它应该根据请求改变它是否仍在处理/结束/失败等等。cellForRow
的事情,那么你做错了,因为你有tableView cell.radioButton.tag = indexPath
的方法。哪个可以说你准确的indexPath 如果您处理来自单元格的一些触摸 - 您应该创建单元格的委托,该单元格将向您的ViewController发送触摸发生的单元格
.indexPath(forCell: UICollectionViewCell)
结束我的讲座 - 你的来源应该完全定义你的细胞。这意味着如果你在模型中有一些标志,表明你是否显示某些东西,你应该写下这样的东西:
func selectRadioButton(inCell cell: UICollectionViewCell) {
guard let indexPath = tableView.indexPath(forCell: cell) else { return }
// do what you need to do & you have your indexPath! = know where touch occurred
}
为什么呢?因为单元格被重用,所有子类属性都保持其状态,即所有UI"设置" (能见度和其他仍然存在)。在google上阅读它有很多关于这个主题的文章,它做得比我好
希望我帮助过你!