swift - 如何在具有特定高度的 TableView 中加载数据

标签 swift tableview

我的应用程序中有一个 TableView 。在此我正在加载我的数据。我希望如果第一个单元格中的数据加载,其高度应为 120,其余单元格的高度应为 50。我还在 didSelect 委托(delegate)中设置条件,如果用户选择单元格,则当前单元格高度增加到 120,其余单元格的高度为 50 .我如何显示我的第一个单元格高度 120 并保持 50 。这是我在 TableView 加载数据时的代码。

 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
    }
}

这就是我在表格 View 加载数据时想要我的第一个单元格的方式, enter image description here

最佳答案

你可以试试

var indexValue = 0

//

  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)


    }


}

关于swift - 如何在具有特定高度的 TableView 中加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51045065/

相关文章:

ios - 如何将对 tableView 的约束设置为 viewController?

Swift 让键盘适应设备屏幕

ios - 将 UISegmentedControl 的结果添加到核心数据实体中的属性

Javafx TableView 未显示所有列中的数据

ios - 如果我更改约束,Xcode 搜索栏不在表标题 View 中

ios - 应用程序终止时执行后台任务

ios - 从推送通知(FCM)获取数据,无需点击通知

arrays - 无法从快速扩展订阅

swift - SCNNode旋转多轴

ios - 将圆角半径应用于 Storyboard 中的特定 UIView 角不适用于所有角