올바른 값에 도달하여 디버그 세션 중에 인쇄했습니다. 그러나 응용 프로그램을 실행할 때 계산 된 값 (newcalory) 특정 테이블 셀 텍스트 필드를 표시하지 않습니다. (일명 .cell.itemTotalCalory.text) 솔루션에 대한 아이디어가 있습니까?테이블 뷰 셀에 업데이트 된 데이터가 표시되지 않습니다.
* 아래 관련 코드 블록을 첨부했습니다.
고마워,
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = ingredientTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! IngredientTableViewCell
cell.ingredientNameTextField.text = ingredients [indexPath.row].ingredientName
cell.numberofItem.text = "1"
let cellcalory = ingredients [indexPath.row].ingredientCalory
cell.itemTotalCalory.text = cellcalory
cell.plusButton.tag = Int(cell.itemTotalCalory.text!)! //indexPath.row
cell.plusButton.addTarget(self, action:#selector(plusAction), for: .touchUpInside)
cell.minusButton.tag = Int(cell.itemTotalCalory.text!)!
cell.minusButton.addTarget(self, action:#selector(minusAction), for: .touchUpInside)
return cell
}
@IBAction func plusAction(sender: UIButton)
{
let cell = ingredientTableView.dequeueReusableCell(withIdentifier: "cell") as! IngredientTableViewCell
let buttonRow = sender.tag
if cell.numberofItem.text == "1" || cell.numberofItem.text != "1"
{
cell.numberofItem.text = "1"
let textValue1 = cell.numberofItem.text
var textValue = Int(textValue1!)
textValue = textValue! + 1
cell.numberofItem.text = String(describing: textValue)
let oldcalory = buttonRow
cell.itemTotalCalory.text = String (((textValue! * Int(oldcalory)) + Int(oldcalory)))
let newcalory = cell.itemTotalCalory.text
refresh(newcalory: newcalory!);
}
}
func refresh(newcalory :String)
{
let cell = ingredientTableView.dequeueReusableCell(withIdentifier: "cell") as! IngredientTableViewCell
cell.itemTotalCalory.text = newcalory
DispatchQueue.main.async {
self.ingredientTableView.reloadData()
}
}
업데이트 된 데이터를 얻으려면 그 perticular 셀을 다시로드해야합니다. –
tableview는 재사용 성의 개념에 따라 작동합니다. 새 셀을 만들거나 다시 사용하는 테이블을 다시로드하십시오. 이러지 마. cell.itemTotalCalory.text = newcalory.instead이 새로운 calory를 배열이나 변수에 저장 한 다음 cell.itemTotalCalory.text에 값을 설정하십시오. – commando24
UITableView가 어떻게 작동하는지 잘 이해하지 못했을 것 같습니다. plusAction에서는 재료 배열에 새 항목을 추가 한 다음 테이블을 호출하여 날짜를 다시로드해야합니다 (새로 고침시 수행 할 작업). 테이블 뷰는 데이터 소스 덕분에 하나의 여분의 아이템이 있다는 것을 알게 될 것이고 추가 셀을 렌더링 할 것입니다. –