2016-08-28 2 views
0

ViewController에서 tableView를 설정했습니다. 이 테이블 뷰에는 1 섹션, 2 프로토 타입 셀이 있습니다. 하나는 섹션의 행 수를 반환하는 데 사용되고 다른 하나는 사용자 지정 헤더를 표시하는 데 사용됩니다. 헤더의 레이블에로드하려는 데이터는 행이 선택 될 때마다 생성됩니다. 문제는 ViewController가 처음로드 될 때마다 행이 선택 될 때마다 데이터가 헤더의 레이블에서 업데이트되지 않는다는 것입니다. 뒤로 단추를 누르고 머리글을 포함하는 ViewController로 돌아 가면 머리글에 첨부 된 레이블로 데이터가 업데이트됩니다. 내가 뭘 잘못하고 있니?tableView 헤더가 데이터를 업데이트하지 않습니다. Swift

셀 didSelectRowAtIndexPath 방법에서 사용자 탭이 호출 될 때마다
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return frequency.count 
} 

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 


func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let headerCell = tableView.dequeueReusableCellWithIdentifier("customheader") as! CustomHeader 

    headerCell.dateHeaderlabel.text = StructS.headerDate 
    headerCell.hourHeaderlabel.text = StructS.headerHours + "-" 
     headerCell.totalHoursHeaderlabel.text = String(StructS.numberHours) 
     headerCell.priceHeaderlabel.text = String(StructS.price) 
     headerCell.backgroundColor = UIColor(red:72/255,green:141/255,blue:200/255,alpha:0.9) 
    tableView.reloadSections(NSIndexSet(index: 0), withRowAnimation: .None) 
     return headerCell 
} 


func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    return 70.0 
} 

BeforeAfter

+0

데이터를 업데이트 한 후 테이블 뷰를 다시로드해야합니다. –

+0

@UmairAfzal 만약'self.tableView.reloadSections (NSIndexSet (index : 0), withRowAnimation : .None)'하면 '또 다른 오류 :'예상 선언문'이 생깁니다. – bibscy

+0

@bogdanbarbulescu 코드 –

답변

1

. 따라서 NSUserDefaults에 선택된 값을 저장 한 다음 사용자 기본값에서 해당 값을 가져 와서 섹션 헤더를 불러올 수 있습니다. 이 방법으로 테이블 뷰로 돌아올 때 섹션 헤더에서 마지막으로 선택된 행의 데이터를 볼 수 있습니다.

+0

NSUserDefaults를 사용하면 섹션 헤더에 더 많은 레이블이 있으므로 많은 코드를 작성해야합니다. 함수를 사용하여 ViewDidLoad에서 내 데이터를 업데이트하는 좀 더 우아한 방법이 있는지 궁금 해서요 ... – bibscy

+0

예, 우아한 방법이 있습니다. 이미 NSUserDefaults를 사용했기 때문에 NSUserDefaults에 대해 말씀 드렸습니다. –

+0

귀하의 답변을 수락했습니다. 시간이 있다면 ViewDidLoad의 헤더 섹션에 데이터를로드하는 방법에 대한 우아한 방법으로 답변을 업데이트하십시오. 다른 사람들도 유용한 두 번째 솔루션을 찾을 수 있기를 바랍니다. 많은 감사합니다 – bibscy

0

UIViewController에 UITableView 개체가있는 경우 Main.storyboard에서 UIViewController를 dataSource로 설정하고 위임합니다. UIViewController의 하위 클래스에서 self를 테이블 뷰의 대리자 및 데이터 소스로 설정합니다. 당신이 데이터를 업데이트 할 필요가 그래서있는 viewDidLoad() FUNC 재정의 { self.tableView.delegate는 = 자기 self.tableView.dataSource = 자기 }는의 ViewController가 처음로드

는, 헤더 레이블은 업데이트되지 않습니다 이전에 선택된 행 (NSUserDefaults에 저장 됨)에 따라 생성 된 정보가 헤더에 포함됩니다.

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated) 

    // retrieve indexPathForCellSelected from NSUserDefaults 
    if let retrievedIndexPath = NSUserDefaults.standardUserDefaults().dataForKey(indexKey) { 
if let data1 = NSKeyedUnarchiver.unarchiveObjectWithData(retrievedIndexPath) as? NSIndexPath { 
    indexPathForCellSelected = data1 

    // inform the delegate that the row has already been selected 
      self.tableView(self.tableView, didSelectRowAtIndexPath: indexPathForCellSelected!) 

} 
    // handle the selection of the row so as to update the values of labels in section header 
    if indexPathForCellSelected == nil { 
     // construct an indexPath for the row we want to select when no previous row was selected (not already saved in NSUserDefaults) 
      let rowToSelect:NSIndexPath = NSIndexPath(forRow: 1, inSection: 0) 

     /* select the row at `rowToSelect` indexPath. This will just register the selectd row, However,the code that you have in tableView:didSelectRowAtIndexPath: is not yet executed because the delegate for the tablewView object in the ViewController has not been called yet. */ 

     self.tableView.selectRowAtIndexPath(rowToSelect, animated: true, scrollPosition: UITableViewScrollPosition.None) 

     // inform the delegate that the row was selected 
     // stackoverflow.com/questions/24787098/programmatically-emulate-the-selection-in-uitableviewcontroller-in-swift 
      self.tableView(self.tableView, didSelectRowAtIndexPath: rowToSelect) 
    } 

} 
+0

@ Umair Afzal 이전에 언급 한 문제에 대한 답변을 추가했습니다. 이 점에 대해 의견이 있으시면 공유하십시오. – bibscy

관련 문제