2015-10-15 55 views
1

我有UITableView掛鉤到一個數組。 「雞蛋」,「牛奶」,「A」,「B」,「C」,「D」,「E」,「F」,「G」,「H」,「 I「,」J「,」K「,」L「,」M「,」N「,」O「] 但是,當我在模擬器中運行應用程序時,它不允許我滾動到結尾。這是爲什麼?我只能滾動到字母「I」,看到「J」的一半,但我無法進一步向下滾動。在func tableView中,它顯示正確的數組大小,所以我知道這些項目都在那裏,但不能滾動到最後。以下是屏幕截圖。謝謝。 screen shot screen shot 2Swift UITableView不顯示所有結果

這裏的代碼

import UIKit 

class TestTableViewController: UITableViewController { 
    var shoppingList: [String] = ["Eggs", "Milk","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O"] 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     print("test") 
     // Uncomment the following line to preserve selection between presentations 
     // self.clearsSelectionOnViewWillAppear = false 

     // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
     // self.navigationItem.rightBarButtonItem = self.editButtonItem() 
    } 

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

    // MARK: - Table view data source 

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     // #warning Incomplete implementation, return the number of sections 
     return 1 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     // #warning Incomplete implementation, return the number of rows 
     print(shoppingList.count) 
     return shoppingList.count 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("TestCell", forIndexPath: indexPath) 

     // Configure the cell... 
     cell.textLabel!.text = shoppingList[indexPath.row] 
     // Configure the cell... 
     print("here") 
     return cell 
    } 
+1

我們怎樣才能幫助你,如果你不發佈您的代碼? –

+0

謝謝,我剛剛發佈了代碼。 – Julia

+0

你在哪裏設置tableView的大小?如果tableview比屏幕長,你將不會看到最後幾個元素 –

回答

0

朱莉婭,這似乎表視圖比屏幕本身長。如果您進入故事板並縮短桌面視圖的長度,您將可以看到其餘部分,因爲屏幕的大小相同或更短。

您可以添加約束條件,這些約束條件會自動對齊以適應屏幕的大小。

如果你想通過代碼來做到這一點,試試這個在您的viewDidLoad()

tableView.frame.size = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) 
+0

嗨,我似乎無法拖動tableview,我該如何設置它的大小?如何添加會自動捕捉到合適的約束,因爲最終我需要用動態數據填充它。另外,當我嘗試在代碼中設置它的大小時,我得到一個錯誤,它說「無法爲類型CGSize指定一個類型'CGRect' – Julia

+0

在這種情況下,輸入它更加困難。超級簡單,而不是打字。 – whereisleo