繁体   English   中英

在UITableView中隐藏单元格(在Xamarin中)

[英]Hiding cell in UITableView (in Xamarin)

我想根据某个单元格是否有针对特定用户的任何数据来隐藏它。 我当前的方法导致错误the requested operation resulted in a stack overflow

public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        ...
        cell.TextLabel.Text = items.Keys.ElementAt(indexPath.Row);
        cell.DetailTextLabel.Text = items[items.Keys.ElementAt(indexPath.Row)];

        if (string.IsNullOrEmpty(cell.DetailTextLabel.Text)){
            cell.Hidden = true;
            cell.Tag = 3;
        }
        return cell;
    }

public override nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
    {
        UITableViewCell cell = tableView.CellAt(indexPath); //ERROR HERE
        if (cell.Tag == 3)
        {
            return 0;
        }
        return base.GetHeightForRow(tableView, indexPath);
    }

如何避免此错误并正确隐藏行?

我的猜测是GetCell()调用GetHeightForRow() ,后者调用GetCell()调用GetHeightForRow() ……这就是堆栈溢出的来源。

您不应使用视觉表示形式(=单元格)来确定行是否应可见。 您的(数据)模型应对此负责。 换句话说:无论您的items字典包含什么对象类型(也许是Person类型-我都不知道),它都应该具有IsVisible属性或类似属性。 然后在GetHeightForRow()访问该项目并检查属性,并确定行高是多少,并为不可见的行返回0。

顺便说一句:我不知道你的代码上面(*)的背景下,但一般你应该叫base.GetHeightForRow() 该方法本身就是作为ObjectiveC协议实现的委托的一部分。 这意味着没有base

(*)如果从UITableViewSourceUITableViewDataSource派生,则在调用base时没有副作用,但是如果直接在UITableViewController实现方法,则可能会看到You_Should_Not_Call_Base_Exception

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM