将UITableView划分为固定数量的单元格

Pas*_*cal 3 uitableview ios swift

我创建了一个UIViewController,它有一个标题并包含一个UITableView.现在我想将UITableView的整个高度划分为6个单元格.我尝试了tableView.frame.size.height,然后设置tableView.rowHeight = tableViewHeight / 6.但是当我启动应用程序时,表视图的底部仍然留有一些空间.还有另外一种方法吗?

hgw*_*tle 6

我建议从tableView:heightForRowAtIndexPath:委托方法返回单元格高度,如下所示:

let numOfRows = 7

...

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return numOfRows
}

...

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return tableView.frame.size.height / CGFloat(numOfRows)
}
Run Code Online (Sandbox Code Playgroud)

使用该方法,您可以直接访问tableView您可以将其高度除以所需行数的位置.返回该值以设置tableView的行高.