swift 2如何在viewcontroller中删除tableview中的空行
是否有方法重置tableview高度,以便不显示空行。例如,tableview显示3行,但只有一行具有实际数据。我希望tableview缩小它的大小,这样只有一行显示我认为您需要在执行swift 2如何在viewcontroller中删除tableview中的空行,swift,swift2,tableview,Swift,Swift2,Tableview,是否有方法重置tableview高度,以便不显示空行。例如,tableview显示3行,但只有一行具有实际数据。我希望tableview缩小它的大小,这样只有一行显示我认为您需要在执行func tableview(tableview:UITableView,numberOfRowsInSection:Int)->Int之前删除空数据 //编辑 我给你这个可能的解决方案,也许有更多的方法,你可以试试这个 class MyViewController: UITableViewDataSource,
func tableview(tableview:UITableView,numberOfRowsInSection:Int)->Int之前删除空数据
//编辑
我给你这个可能的解决方案,也许有更多的方法,你可以试试这个
class MyViewController: UITableViewDataSource, UITableViewDelegate {
private var realHeight = 0
private var data : [String] = []
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MyCellID")!
cell.textLabel?.text = data[indexPath.row]
self.realHeight += self.tableView.rowHeight //Increase real height value.
return cell
}
func loadData() { //This function reload the data
self.realHeight = 0
self.tableView.reloadData()
//Update tableViewHeight here, use self.realHeight. You can use constraints or update directly the frame.
}
}
解决方案是使用一个计数器来表示所有可见行高度的总和。e、 g如果您有n单元格,那么您的高度将是self.tableView.rowHeight
*n,那么您将永远拥有有效高度
我建议您为表格高度创建一个约束,当单元格更改时,您只需要更改约束的常数,比更改框架更容易。我认为您需要在执行func tableView(tableView:UITableView,numberOfRowsInSection:Int)->Int之前删除空数据
//编辑
我给你这个可能的解决方案,也许有更多的方法,你可以试试这个
class MyViewController: UITableViewDataSource, UITableViewDelegate {
private var realHeight = 0
private var data : [String] = []
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MyCellID")!
cell.textLabel?.text = data[indexPath.row]
self.realHeight += self.tableView.rowHeight //Increase real height value.
return cell
}
func loadData() { //This function reload the data
self.realHeight = 0
self.tableView.reloadData()
//Update tableViewHeight here, use self.realHeight. You can use constraints or update directly the frame.
}
}
解决方案是使用一个计数器来表示所有可见行高度的总和。e、 g如果您有n单元格,那么您的高度将是self.tableView.rowHeight
*n,那么您将永远拥有有效高度
我建议您为表格高度创建一个约束,当单元格更改时,您只需要更改约束的常数,这比更改框架更容易。我猜您有这样一个视图控制器
class Controller: UITableViewController {
private var data: [String?] = ["One", nil, "Three", nil, nil]
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MyCellID")!
cell.textLabel?.text = data[indexPath.row]
return cell
}
}
由于您的模型(data
,在我的示例中)包含nil
值,因此您将获得如下表视图
class Controller: UITableViewController {
private var data: [String?] = ["One", nil, "Three", nil, nil]
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MyCellID")!
cell.textLabel?.text = data[indexPath.row]
return cell
}
}
一个
_
三
_
_
删除空行
现在您想要这样的表视图,对吗
一个
三
筛选您的模型
由于UI(表视图)只是模型的一种表示形式,因此需要更改模型
这很容易
class Controller: UITableViewController {
private var data: [String?] = ["One", nil, "Three", nil, nil]
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MyCellID")!
cell.textLabel?.text = data[indexPath.row]
return cell
}
override func viewDidLoad() {
data = data.filter { $0 != nil }
super.viewDidLoad()
}
}
如您所见,在viewDidLoad()
中,我从data
数组中删除了账单值。现在,您将只获得实际值的单元格。我猜您有这样一个视图控制器
class Controller: UITableViewController {
private var data: [String?] = ["One", nil, "Three", nil, nil]
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MyCellID")!
cell.textLabel?.text = data[indexPath.row]
return cell
}
}
由于您的模型(data
,在我的示例中)包含nil
值,因此您将获得如下表视图
class Controller: UITableViewController {
private var data: [String?] = ["One", nil, "Three", nil, nil]
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MyCellID")!
cell.textLabel?.text = data[indexPath.row]
return cell
}
}
一个
_
三
_
_
删除空行
现在您想要这样的表视图,对吗
一个
三
筛选您的模型
由于UI(表视图)只是模型的一种表示形式,因此需要更改模型
这很容易
class Controller: UITableViewController {
private var data: [String?] = ["One", nil, "Three", nil, nil]
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MyCellID")!
cell.textLabel?.text = data[indexPath.row]
return cell
}
override func viewDidLoad() {
data = data.filter { $0 != nil }
super.viewDidLoad()
}
}
如您所见,在viewDidLoad()
中,我从data
数组中删除了账单值。现在您将只获得实际值的单元格。您应该发布tableview数据源和委托方法的代码。答案将取决于您如何设置这些方法。因为我不知道如何设置,所以没有编写代码。您应该发布tableview的数据源和委托方法的代码。答案将取决于你如何设置这些方法。因为我不知道怎么做,所以没有写yetNo的代码,事实并非如此。我的例子是,在故事板中,大约有6行,而在现实世界中,只有2行。因此,当应用程序运行时,它会留下4个空行。我想做的是删除这4个空行,并将tableview高度缩小到第二行的末尾。@user584819:所以在故事板中,您确实为tableview设置了Content=Static单元格,对吗?不,它是动态的cells@user584819:在您之前的评论中,您在故事板中说了,大约有6行。但现在你说它有动态单元格
。这怎么可能?您的表中的行数是通过代码在Storyboard o中定义的吗?仅举一个例子。使用动态单元格,tableview的大小为w=280,h=500。但当数据填充到tableview中时,只有2行的长度约为h=80。如何将tableview高度从500缩小到80,以便在最后一行之后没有空的空间。不,情况并非如此。我的例子是,在故事板中,大约有6行,而在现实世界中,只有2行。因此,当应用程序运行时,它会留下4个空行。我想做的是删除这4个空行,并将tableview高度缩小到第二行的末尾。@user584819:所以在故事板中,您确实为tableview设置了Content=Static单元格,对吗?不,它是动态的cells@user584819:在您之前的评论中,您在故事板中说了,大约有6行。但现在你说它有动态单元格
。这怎么可能?您的表中的行数是通过代码在Storyboard o中定义的吗?仅举一个例子。使用动态单元格,tableview的大小为w=280,h=500。但当数据填充到tableview中时,只有2行的长度约为h=80。如何将tableview高度从500缩小到80,以便在最后一行之后没有空的空间。不,情况并非如此。我的例子是,在故事板中,大约有6行,而在现实世界中,只有2行。因此,当应用程序运行时,它会留下4个空行。我喜欢做的是删除这4个空行,并将tableview高度缩小到第二行的末尾。您好@user584819,我没有正确理解您的意思:(你能给我提供一些代码吗?我不能,因为我不知道该怎么做。想想一个简单的例子。在故事板的UIViewController一侧,你添加了一个大小为w=280和h=500的uitableview。在代码中,你只填充了2行/单元格数据,所以