Ios UITableViewCell的动态维度可以,但如果为空,如何隐藏它们?
我有一个带有3个原型单元格的Ios UITableViewCell的动态维度可以,但如果为空,如何隐藏它们?,ios,swift,uitableview,ios8,Ios,Swift,Uitableview,Ios8,我有一个带有3个原型单元格的UITableView(例如,第一个单元格:图像,第二个单元格:描述,3个链接,…) 如果某个单元格的后端数据为空,我想将其隐藏(例如,如果没有图像,则隐藏第一个单元格)。为了做到这一点,我用以下方式重写了heightforrowatinexpath函数: override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFlo
UITableView
(例如,第一个单元格:图像,第二个单元格:描述,3个链接,…)
如果某个单元格的后端数据为空,我想将其隐藏(例如,如果没有图像,则隐藏第一个单元格)。为了做到这一点,我用以下方式重写了heightforrowatinexpath
函数:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
switch (indexPath.row) {
case 0:
if event?.photo_urls.count == 0{
return 0
}
else{
return 80.0
}
case 1:
if event?.description == ""{
return 0
}
else{
return 90.0
}
default:
return 100.0
}
}
然后通过这样做来隐藏细胞
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
switch (indexPath.row) {
case 0:
let cell = tableView.dequeueReusableCellWithIdentifier("PhotoCell", forIndexPath: indexPath) as! UITableViewCell
if event?.photo_urls.count != 0 {
// ...
}
else{
cell.hidden = true
}
return cell
case 1:
let cell = tableView.dequeueReusableCellWithIdentifier("DesCell", forIndexPath: indexPath) as! UITableViewCell
if event?.description != "" {
// ...
}
else{
cell.hidden = true
}
return cell
default:
let cell = tableView.dequeueReusableCellWithIdentifier("PhotoCell", forIndexPath: indexPath) as! UITableViewCell
return cell
}
}
直到这里没有问题,它工作正常!
现在的问题是,我想根据单元格内容(例如描述高度)创建单元格动态。为了做到这一点,我使用了
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 80.0
}
如果我在dexpath中为row注释高度,单元格实际上是动态的,但我不能再隐藏它们了
对于如何隐藏空单元格并根据其内容应用自动维度,您有什么建议吗?为此,我建议您在将数组显示在表视图中之前对其进行排序。隐藏手机不是一个好主意,根据苹果公司的建议也是不好的。因此,除了隐藏单元格,您可以做一件事:从数组中删除索引。通过这种方式,您可以始终在表中显示数据,它将正常工作。因此,不要试图隐藏单元格,只要从数组中弹出索引即可。假设您有动态数据,并且希望在tableview中显示它,因此您需要创建一个要显示的数据数组
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet
var tableView: UITableView
var items: [String] = ["We", "Heart", "nothing" ,"" ,"imageurl", "", "xyz"]
override func viewDidLoad() {
super.viewDidLoad()
reloadTableAfterSorting()
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
func reloadTableAfterSorting(){
for var i = 0; i < self.items.count; i++
{
if self.items[i] == ""{
self.items.removeAtIndex(2)
}
}
self.tableView.reloadData()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel?.text = self.items[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("You selected cell #\(indexPath.row)!")
}
}
类ViewController:UIViewController、UITableViewDelegate、UITableViewDataSource{
@IBOutlet
var tableView:UITableView
变量项:[String]=[“我们”、“心”、“无”、“图像URL”、“xyz”]
重写func viewDidLoad(){
super.viewDidLoad()
重新加载TableAfterOrting()
self.tableView.registerClass(UITableViewCell.self,forCellReuseIdentifier:“单元格”)
}
func reloadTableAfterSorting(){
对于变量i=0;iInt{
返回self.items.count;
}
func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
变量单元格:UITableViewCell=self.tableView.dequeueReusableCellWithIdentifier(“单元格”)作为UITableViewCell
cell.textLabel?.text=self.items[indexPath.row]
返回单元
}
func tableView(tableView:UITableView,didSelectRowAtIndexPath:nsindepath){
println(“您选择了单元格(indexPath.row)!”)
}
}
谢谢您的回答。抱歉,我对iOS开发相当陌生:从哪个数组中删除索引?不客气。所以没有数组。这是一个静态的tableview好的,让我给你写一封信,请检查一下,如果你明白了,然后接受答案,这样对我也会有帮助@valectank再次感谢你的解决方案,但我想我不能把它应用到我的案例中,因为我所有的单元格都是定制的(标签、图像、一个或多个标签、地图的配置…)兄弟,你可以很容易地应用它,我给了你一种方法,你可以这样做,这会让你见鬼,我建议你试试,这会帮助你理解流程,或者给我你的完整代码,我会根据你的需要为你做,并描述你想要显示的值,如果有帮助,请接受答案