2017-01-23 1 views
0

올바른 값에 도달하여 디버그 세션 중에 인쇄했습니다. 그러나 응용 프로그램을 실행할 때 계산 된 값 (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() 
     }  
    } 
+0

업데이트 된 데이터를 얻으려면 그 perticular 셀을 다시로드해야합니다. –

+1

tableview는 재사용 성의 개념에 따라 작동합니다. 새 셀을 만들거나 다시 사용하는 테이블을 다시로드하십시오. 이러지 마. cell.itemTotalCalory.text = newcalory.instead이 새로운 calory를 배열이나 변수에 저장 한 다음 cell.itemTotalCalory.text에 값을 설정하십시오. – commando24

+1

UITableView가 어떻게 작동하는지 잘 이해하지 못했을 것 같습니다. plusAction에서는 재료 배열에 새 항목을 추가 한 다음 테이블을 호출하여 날짜를 다시로드해야합니다 (새로 고침시 수행 할 작업). 테이블 뷰는 데이터 소스 덕분에 하나의 여분의 아이템이 있다는 것을 알게 될 것이고 추가 셀을 렌더링 할 것입니다. –

답변

0

해결 방법을 찾았습니다. 아래 나열된 줄은 쓸모가 없습니다.

let cell = ingredientTableView.dequeueReusableCell(withIdentifier: "cell") as! IngredientTableViewCell 
     cell.itemTotalCalory.text = newcalory 

plusAction 함수 내에서 새로운 값으로 성분 배열을 업데이트했는데 문제가 해결되었습니다. 모든 게시물 주셔서 감사합니다.

0

당신이 할 일은 ingredients 배열의 값을 업데이트 한 후 UI에이를 반영 할 ingredientTableView.reloadData()를 호출하는 것입니다. refresh 방법에 dequeueReusableCell(withIdentifier:)를 호출

은 당신이 뭘 하려는지 예상 작동하지 않습니다 같이이 행에 셀을 할당 할 때 성능상의 이유로

, 테이블 뷰의 데이터 소스는 일반적으로 재사용있는 UITableViewCell은 객체한다 tableView (_ : cellForRowAt :) 메서드에 있습니다. 테이블 뷰는 데이터 소스가 재사용으로 표시된 UITableViewCell 개체의 큐 또는 목록을 유지 관리합니다. 테이블 뷰에 대해 새 셀을 제공하라는 메시지가 표시되면 데이터 소스 개체에서이 메서드를 호출합니다.이 메서드는 기존 셀이있는 경우 기존 셀을 제거하거나 이전에 등록한 nib 파일 인 클래스를 사용하여 새 셀을 만듭니다. 재사용에 셀을 사용할 수없고 클래스 또는 nib 파일을 등록하지 않은 경우이 메서드 은 nil을 반환합니다. 당신이있는 tableView에서 특정 셀을 얻고 싶은 것을 확신하는 경우 또한

func refresh() { 
    // updating ingredients array upon reqs satisfaction... 
    // and then: 
    ingredientTableView.reloadData() 

    // nameOfYourRefreshControl.endRefreshing() 
} 

는, 당신은 cellForRow(at:) 인스턴스 방법을 사용할 수 있습니다 :에

그래서, 새로 고침 방법은 비슷해야합니다 :

지정된 인덱스 경로에있는 표 셀을 반환합니다.

func refresh() { 
    let cell = ingredientTableView?.cellForRow(at: YOUR_INDEX_PATH) 

    //... 
} 

희망이 도움이되었다.

+0

Ahmad 안녕하세요, 귀하의 회신에 감사드립니다. 당신은 indexPath에 대한 권리가 있지만 indexPath.row를 cellForRowAt 메서드로 보내는데 어려움이 있습니다. plusAction에 칼로리를 보내려면 '태그'방법이 필요합니다. 인덱스 경로 행을 배열에 저장하려고하면 오류가 발생합니다. 첫째, 나는 plusAction에 indexPath.row를 보낸 다음 함수를 새로 고쳐야합니다. 의견 있으십니까? – gozdebal

+0

성취하고자하는 것은 무엇입니까? 내가 이해할 수있는 것은 당신이 각 셀에 버튼을 가지고 있고 어떤 버튼이 두드려 졌는지에 따라 값을 편집하고 싶다면 버튼에 타겟을 추가하고 그 태그가 무엇인지에 따라 값을 업데이트해야한다. 배열에서 - 예를 들어 -... –

관련 문제