发布
社区首页 >问答首页 >如何基于用户选择过滤动态创建的表视图

如何基于用户选择过滤动态创建的表视图
EN

Stack Overflow用户
提问于 2015-02-20 12:10:18
回答 1查看 461关注 0票数 0

我希望根据用户设置的字符串值筛选表视图。表视图由一个由几个组件(图像、标签等)组成的单元格生成。所有这些都位于数组和字典的“混乱”中。我正在使用下面的代码。当我过滤时,我只是隐藏单元格,但它们仍然占据着表视图中的空间。应用过滤器的最佳方法是什么,从而只得到符合要求的单元?我正在筛选的领域就是主题。

代码语言:javascript
代码运行次数:0
复制
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
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-20 12:21:07

您需要在DataSource方法中跟踪单元格的数量,该方法询问当前区段的单元格数。

代码语言:javascript
代码运行次数:0
复制
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // Here
}

如果你只做cell.hidden = true空间就不会消失,它只会隐藏内容。这是因为在其他方法中计算出的所有单元格布局。

如果要隐藏此类单元格的空间,可以选择以下几种解决方案:

  1. (最好)计算非隐藏单元格的数量,在func tableView(tableView: UITableView, numberOfRowsInSection section: Int)中返回新计数,并使用func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)信息重新计算字典
  2. 这不是很好的解决办法,但对于你的问题可以发生。在方法中,要求tableView单元格高度func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat只返回隐藏单元格的0。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28628668

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档