Как загрузить данные в виде таблицы с определенной высоты - PullRequest
0 голосов
/ 26 июня 2018

В моем приложении есть табличное представление. В этом я загружаю свои данные. Я хочу, чтобы, если загрузка данных в первую ячейку, ее высота должна быть 120, а оставшаяся ячейка должна иметь высоту 50. У меня также есть условие в делегате didSelect, что если пользователь выбирает ячейку, текущая высота ячейки увеличивается до 120, а оставшиеся ячейки имеют высоту 50 , Как я могу показать свою первую клетку высотой 120 и оставаясь с 50. Это мой код, когда табличное представление загружает данные.

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

Вот как я хочу свою первую ячейку, когда табличное представление загружает данные, enter image description here

1 Ответ

0 голосов
/ 26 июня 2018

Вы можете попробовать

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)


    }


}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...