Swift 如何在具有特定高度的表视图中加载数据
我的应用程序中有一个表视图。在这里,我正在加载我的数据。我想,如果第一个单元格中的数据加载,其高度应为120,其余单元格的高度应为50。我在didSelect委托中还有一个条件,即如果用户选择单元格,当前单元格高度将增加到120,其余单元格的高度为50。如何显示我的第一个单元格高度为120,剩余高度为50。这是我在表视图加载数据时的代码Swift 如何在具有特定高度的表视图中加载数据,swift,tableview,Swift,Tableview,我的应用程序中有一个表视图。在这里,我正在加载我的数据。我想,如果第一个单元格中的数据加载,其高度应为120,其余单元格的高度应为50。我在didSelect委托中还有一个条件,即如果用户选择单元格,当前单元格高度将增加到120,其余单元格的高度为50。如何显示我的第一个单元格高度为120,剩余高度为50。这是我在表视图加载数据时的代码 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dishNameArray.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if (flag == true && indexPath.row == indexValue) {
return 120
}
else {
return 40
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = cartTableView.dequeueReusableCell(withIdentifier: "cartCell", for: indexPath) as! CartTVC
cell.dishNameLbl.text = dishNameArray[indexPath.row]
cell.eachPriceLbl.text = priceArray[indexPath.row]
cell.quantityLbl.text = "1"
cell.qtyLbl.text = "1"
cell.totalPriceLbl.text = priceArray[indexPath.row]
cell.deleteBtn.tag = indexPath.row
cell.deleteBtn.addTarget(self, action: #selector(crossBtnTapped), for: .touchUpInside)
cell.selectionStyle = .none
return cell
}
@objc func crossBtnTapped(sender: UIButton) {
dishNameArray.remove(at: sender.tag)
let indexPath = IndexPath.init(row: sender.tag, section: 0)
let cell = cartTableView.cellForRow(at: indexPath) as! CartTVC
print(cell)
cartTableView.reloadData()
print(sender.tag)
print("II:\(dishNameArray.count)")
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Yes")
indexValue = indexPath.row
if flag && indexValue == indexPath.row{
// rfpNumberTableView.beginUpdates()
let cell = self.cartTableView.cellForRow(at: indexPath) as! CartTVC
cell.bgView.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
self.cartTableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.none)
// cartTableView.endUpdates()
flag = false
}
else{
let cell = self.cartTableView.cellForRow(at: indexPath) as! CartTVC
cell.bgView.backgroundColor = #colorLiteral(red: 0.9529120326, green: 0.3879342079, blue: 0.09117665142, alpha: 1)
// rfpNumberTableView.beginUpdates()
self.cartTableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.none)
// cartTableView.endUpdates()
flag = true
}
}
这就是我希望在“表视图”加载数据时使用的第一个单元格,
你可以试试
var indexValue = 0
//
//
//
当前的问题是,当在表视图中加载任何数据时,其加载高度为40。我希望在表视图中加载数据时,其第一个单元格的高度应为120,其余单元格的高度应为40。点击单元格,高度会增加到120@Sh_Khanwhy使用标志i have used标志表示我在didselect委托中的状态,每当用户单击高度为40的任何单元格时,该单元格将变为高度为120@当数据添加到tableview中时,如何显示第一个单元格的高度仅为120,其余单元格的高度为40@什乌·坎佩克托。这就是我想要的@如果我从表视图中删除任何单元格,Sh_Khani会崩溃。这是崩溃消息0500 Grabbos[72954:2623545]***由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因是:“尝试从节0中删除第2行,该节在更新之前仅包含2行”@Sh_Khan在从表中删除之前,请确保从数据源阵列中删除,我在我的交叉方法中删除它,你可以检查我的代码@这是我的删除代码@objc func crosstntapped(sender:UIButton){dishNameArray.remove(at:sender.tag)let indexath=indexPath.init(row:sender.tag,section:0)let cell=cartTableView.cellForRow(at:indexath)as!CartTVC print(cell)cartTableView.reloadData()print(sender.tag)print(II:(dishNameArray.count)“)}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == indexValue {
return 120
else {
return 50
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = cartTableView.dequeueReusableCell(withIdentifier: "cartCell", for: indexPath) as! CartTVC
if indexPath.row == indexValue {
cell.bgView.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
}
else {
cell.bgView.backgroundColor = #colorLiteral(red: 0.9529120326, green: 0.3879342079, blue: 0.09117665142, alpha: 1)
}
cell.dishNameLbl.text = dishNameArray[indexPath.row]
cell.eachPriceLbl.text = priceArray[indexPath.row]
cell.quantityLbl.text = "1"
cell.qtyLbl.text = "1"
cell.totalPriceLbl.text = priceArray[indexPath.row]
cell.deleteBtn.tag = indexPath.row
cell.deleteBtn.addTarget(self, action: #selector(crossBtnTapped), for: .touchUpInside)
cell.selectionStyle = .none
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Yes")
if indexValue != indexPath.row {
let oldIndex = IndexPath(row: indexValue , section: 0)
indexValue = indexPath.row
self.cartTableView.reloadRows(at: [indexPath,oldIndex], with: UITableViewRowAnimation.none)
}
}