我希望根据用户设置的字符串值筛选表视图。表视图由一个由几个组件(图像、标签等)组成的单元格生成。所有这些都位于数组和字典的“混乱”中。我正在使用下面的代码。当我过滤时,我只是隐藏单元格,但它们仍然占据着表视图中的空间。应用过滤器的最佳方法是什么,从而只得到符合要求的单元?我正在筛选的领域就是主题。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if filterChoice == "Alle"
{
var cell = tableView.dequeueReusableCellWithIdentifier("NewsCell") as News_TableViewCell
cell.subject!.text = subjects[indexPath.row]
if videoEmbedCode.valueForKey(self.recordIDs[indexPath.row]) != nil
{
var iframeCode: NSString = videoEmbedCode.valueForKey(self.recordIDs[indexPath.row]) as String
var html = "<html><body>\(iframeCode)</body></html>"
cell.webView.loadHTMLString(html, baseURL :nil)
cell.webView.hidden = false
} else if self.imageCache.valueForKey(self.recordIDs[indexPath.row]) != nil
{
cell.newsImage.image = self.imageCache.valueForKey(self.recordIDs[indexPath.row]) as? UIImage
cell.newsImage.hidden = false
} else
{
}
if topics.valueForKey(self.recordIDs[indexPath.row]) != nil
{
cell.topicLabel.text = topics.valueForKey(self.recordIDs[indexPath.row]) as? String
}
else
{
cell.topicLabel.text = ""
}
return cell
}
else if topics.valueForKey(self.recordIDs[indexPath.row]) as? String == filterChoice
{
//Only return cells with specified topic
var cell = tableView.dequeueReusableCellWithIdentifier("NewsCell") as News_TableViewCell
if topics.valueForKey(self.recordIDs[indexPath.row]) != nil
{
cell.topicLabel.text = topics.valueForKey(self.recordIDs[indexPath.row]) as? String
}
else
{
cell.topicLabel.text = ""
}
cell.subject!.text = subjects[indexPath.row]
if videoEmbedCode.valueForKey(self.recordIDs[indexPath.row]) != nil
{
var iframeCode: NSString = videoEmbedCode.valueForKey(self.recordIDs[indexPath.row]) as String
var html = "<html><body>\(iframeCode)</body></html>"
cell.webView.loadHTMLString(html, baseURL :nil)
cell.webView.hidden = false
} else if self.imageCache.valueForKey(self.recordIDs[indexPath.row]) != nil
{
cell.newsImage.image = self.imageCache.valueForKey(self.recordIDs[indexPath.row]) as? UIImage
cell.newsImage.hidden = false
} else
{
}
return cell
}
else
{
var cell = UITableViewCell()
cell.hidden = true
return cell
}
}
发布于 2015-02-20 12:21:07
您需要在DataSource方法中跟踪单元格的数量,该方法询问当前区段的单元格数。
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Here
}
如果你只做cell.hidden = true
空间就不会消失,它只会隐藏内容。这是因为在其他方法中计算出的所有单元格布局。
如果要隐藏此类单元格的空间,可以选择以下几种解决方案:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int)
中返回新计数,并使用func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
信息重新计算字典func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
只返回隐藏单元格的0。https://stackoverflow.com/questions/28628668
复制相似问题