ios - Swift UITableView 不显示所有结果

标签 ios swift uitableview storyboard

我将 UITableView 连接到一个数组。 var shoppingList: [String] = ["鸡蛋", "牛奶","A","B","C","D","E","F","G","H","I ","J","K","L","M","N","O"] 但是,当我在模拟器中运行该应用程序时,它不允许我滚动到末尾。这是为什么?我只能滚动到字母“I”并看到一半的“J”,但无法向下滚动。在 func tableView 中,它显示了正确的数组大小,所以我知道所有项目都在那里,但无法滚动到末尾。下面是屏幕截图。谢谢。 screen shot screen shot 2

这是代码

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
    }

最佳答案

Julia,表格 View 似乎比屏幕本身长。如果您进入 Storyboard并缩短 tableview 的长度,您将能够看到其余部分,因为它与屏幕的尺寸相同或更短。

您可以添加约束,这些约束将自动捕捉以适应任何尺寸的屏幕。

如果你想通过代码来完成,在你的 viewDidLoad() 中试试这个

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

关于ios - Swift UITableView 不显示所有结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33153074/

相关文章:

ios - 保存按钮点击结果在 "unrecognized selector sent to instance"

ios - UserDefaults 保存字典数组

swift - 无法将类型 'Foo!' 的值转换为预期参数类型 'Foo!'

xcode - 解析|云码当前用户

ios - 从核心数据添加新实体后重新加载 UITableView

ios - 当使用多个部分时,当部分数量可能发生变化时,如何对 tableView 单元格的删除/添加进行动画处理

iphone - 核心数据删除最后一个条目

ios - 为什么不能通过使用另一个 viewController 来设置 viewController 的变量值?

ios - 弹跳节点变得疯狂

ios - 使用 Swift 的 iOS 代码结构,所有代码放在哪里?