自动定制自定义单元格

时间:2016-02-23 20:27:16

标签: ios xamarin

我有以下类生成自定义单元格。它工作得很好。但是,我有一个条件,我需要为我的InfoLabel获得比下面定义的更多的空间。我无法处理它?

public WeinerGeneralCellItem (NSString cellId, Short Weiner) : base     (UITableViewCellStyle.Default, cellId)
    {
        this.Weiner = Weiner;

        weinerInfoLabel = new UILabel () {
            TextColor = UIColor.FromRGB (0, 0, 0),
            BackgroundColor = UIColor.Clear,
            TextAlignment = UITextAlignment.Left,
        };

        weinerInfo = new UITextField () {
            TextColor = UIColor.FromRGB (0, 0, 0),
            TextAlignment = UITextAlignment.Right,
            BackgroundColor = UIColor.Clear,
        };

        ContentView.Add (weinerInfoLabel);
        ContentView.Add (weinerInfo);   
    }

    public override void LayoutSubviews ()
    {
        base.LayoutSubviews ();
        weinerInfoLabel.Frame = new CGRect(20, ContentView.Bounds.Height/4, 200, 25);
        weinerInfo.Frame = new CGRect(220, ContentView.Bounds.Height/4, 300, 25);
    }
}

默认情况下,infolabel宽度为200,信息为300,但对于最后一个单元格,我希望infoLabel宽度为350,信息为150.我尝试了以下但是它不起作用。

public override UITableViewCell GetCell (UITableView tableView, NSIndexPath  indexPath)
{
    if (indexPath.Row == 5) {
      cell.weinerInfoLabel.Frame = new CGRect(20, 20, 350, 25);
      cell.weinerInfo.Frame = new CGRect(350, 20, 150, 25);
      cell.weinerInfoLabel.Text = "The value calculated from the server";
      cell.weinerInfo.Tag = 5;
      cell.weinerInfo.Text = "155.00";  
  }
}

1 个答案:

答案 0 :(得分:0)

这是我最终解决的问题,它正在发挥作用。我只是在为相应的单元格设置框架之前检查tag id。

public override void LayoutSubviews ()
{
    base.LayoutSubviews ();
    if (weinerInfo.Tag == 5) 
    {
       weinerInfoLabel.Frame = new CGRect (20, ContentView.Bounds.Height / 4, 350, 25);
       weinerInfo.Frame = new CGRect (350, ContentView.Bounds.Height / 4, 170, 25);
    } 
    else 
    {
        weinerInfoLabel.Frame = new CGRect(20, ContentView.Bounds.Height/4, 200, 25);
        weinerInfo.Frame = new CGRect (220, ContentView.Bounds.Height / 4, 300, 25);
    }
}