2014-10-15 4 views
0

내 테이블 뷰 크기 조정과 함께 몇 가지 문제를 데를 클릭 할 때만 - 그것은 보이는 페이지가로드 될 때 :의 TableView 크기 조정 - 버튼을

enter image description here

후 버튼을

enter image description here

을 클릭하면

버튼 클릭으로 표가 올바른 크기로 렌더링됩니다. 처음부터 어떻게하면 좋을까요? 더 깔끔하게 보일 것입니다.

클래스의 코드는 다음과 같습니다.

// 
// FirstViewController.swift 
// Intercultural Collaboration 
// 
// Created by Ricki Lambert on 06/10/2014. 
// Copyright (c) 2014 com.kentapps. All rights reserved. 
// 

import UIKit 

class QuizViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate{ 

    var countries:Array<Answer> = []; 
    var selected = -1; 
    var positionOfSelected = 1; 

    @IBOutlet var next: UIBarButtonItem! 
    @IBOutlet var tableView: UITableView! 


    @IBAction func next(sender: AnyObject) { 

    } 

    func checkOkToProceed() -> Bool { 
     positionOfSelected = 1; 
     var result = false; 

     for answer in countries{ 
      if(answer.selected){ 
       result = true; 
       break; 
      } 
      positionOfSelected++; 
     } 
     return result; 
    } 

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { 

     if(sender.tag > 0 && !self.checkOkToProceed()){ 
      //the user has not selected an answer so 
      //we need to show them a prompt 

      var alert = UIAlertController(title: "Unanswered Question", message: "Please select an answer!", preferredStyle: UIAlertControllerStyle.Alert) 
      alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)) 
      self.presentViewController(alert, animated: true, completion: nil); 
     } 
    } 


    override func viewDidLoad() { 
     super.viewDidLoad(); 

     tableView.reloadData(); 
     //load answers to countries array 
     self.tableView.allowsMultipleSelection = false; 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 



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

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return self.countries.count; 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     let cell = self.tableView.dequeueReusableCellWithIdentifier("CountryCell", forIndexPath: indexPath) as UITableViewCell 

     cell.textLabel?.text = self.countries[indexPath.row].answerName; 
     cell.textLabel?.textAlignment = NSTextAlignment.Center; 

     let countryImage = String(self.countries[indexPath.row].answerName) + ".png"; 
     cell.imageView?.image = UIImage(named: countryImage); 


     if(countries[indexPath.row].selected){ 
      let imageName = "tick.png"; 
      let image: UIImageView = UIImageView(image: UIImage(named: imageName)); 
      cell.accessoryView = image; 
     }else{ 
      let image: UIImageView = UIImageView(); 
      cell.accessoryView = image; 
     } 

     tableView.sizeToFit(); 
     return cell; 
    } 


    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) { 

     countries[indexPath.row].setSelected(true); 

     if(selected != -1){ 
      countries[selected].setSelected(false); 
     } 
     selected = indexPath.row; 
     tableView.reloadData(); 
    } 

    func alertView(alertView: UIAlertView!, clickedButtonAtIndex buttonIndex: Int) { 

     if (buttonIndex == 0) { 
      //Do something 
     } 
    } 

} 
+0

왜 'cellForRowAtIndexPath'에서 각 셀을 만든 후에'tableView.sizeToFit()'를 호출하는지 혼란 스럽습니까? 'tableView.reloadData()'바로 다음에'viewDidLoad'에서'sizeToFit()'를 호출하려고 했습니까? – zisoft

+0

예, 불행히도 작동하지 않습니다 : ( – Biscuit128

+0

'viewDidAppear'에서'sizeToFit'를 호출하여 차이가 있는지 확인하십시오 – zisoft

답변

0

최근에이 문제가있었습니다. 여기에 우아한 해결책이 있습니다 :

tableView.tableFooterView = UIView() 
+0

불행히도 이것도 작동하지 않습니다. – Biscuit128

+0

'viewDidLoad()'에 넣었습니까? – Mundi

+0

나는 그랬습니다 - 예 - after super.viewDidLoad – Biscuit128

관련 문제