如何在Xamarin iOS中自动调整UITableView的行高大小 [英] How to resize the row height of a UITableView automatically in xamarin ios

查看:147
本文介绍了如何在Xamarin iOS中自动调整UITableView的行高大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是xamarin.ios的新手,我想根据内容长度调整行高的大小.我该怎么办.

I am new in xamarin.ios I want to resize the row height according to the content length. How can I do that.

这是我的ViewController.Cs

Here is my ViewController.Cs

 public override void ViewWillAppear(bool animated)
    {
        base.ViewWillAppear(animated);

        nfloat _width = UIScreen.MainScreen.Bounds.Width / 3;
        DataBinding();

        _table = new UITableView(new CGRect(0, 0, View.Frame.Width, View.Frame.Height));
        _table.Source = new DetailsTableViewSource(_caseDesign);
        _table.RowHeight = UITableView.AutomaticDimension;
        _table.EstimatedRowHeight = 100f;         
        _table.ReloadData();
        View.AddSubview(_table);
    }

DetailsTableViewSource.Cs

DetailsTableViewSource.Cs

   public class DetailsTableViewSource : UITableViewSource
{
    public List<CaseDetails> tabledata;

    public DetailsTableViewSource(List<CaseDetails> items)
    {
        tabledata = items;
    }

    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        NSString cellIdentifier = new NSString();
        var cell = tableView.DequeueReusableCell(cellIdentifier) as CaseDetailsTableCell;
        if (cell == null)
            cell = new CaseDetailsTableCell(cellIdentifier);

        cell.UpdateCell(tabledata[indexPath.Row].Title
                , tabledata[indexPath.Row].Description                  
                );
        return cell;
    }

    public override nint RowsInSection(UITableView tableview, nint section)
    {
        return tabledata.Count;
    }
}

CaseDetailsTableCell.cs

CaseDetailsTableCell.cs

  public class CaseDetailsTableCell:UITableViewCell
{
    UILabel _qst1, _answer1, _seperator;
    nfloat _width;

    public CaseDetailsTableCell(NSString cellId) : base(UITableViewCellStyle.Default, cellId)
        {
         _width = UIScreen.MainScreen.Bounds.Width / 3;

        _qst1 = new UILabel();

        _qst1.Font = _qst1.Font.WithSize(15);

         _seperator = new UILabel();          
        _seperator.TextAlignment = UITextAlignment.Center;
        _seperator.Text = ":";
        _seperator.Font = _seperator.Font.WithSize(20);

        _answer1 = new UILabel();

        _answer1.Font = _answer1.Font.WithSize(15);

        ContentView.AddSubviews(new UIView[] { _qst1, _seperator, _answer1 });
    }

    public void UpdateCell(string Quetion, string Answer)
    {
        _qst1.Text = Quetion;
        _answer1.Text = Answer;
    }

    public override void LayoutSubviews()
    {
        base.LayoutSubviews();
        _qst1.Frame = new CGRect(10, 10, _width + 10, 30);
        _qst1.Lines = 0;
        _qst1.SizeToFit();
        _qst1.LineBreakMode = UILineBreakMode.Clip;
        _qst1.TextAlignment = UITextAlignment.Left;

        _answer1.Frame = new CGRect(_width * 2, 10, UIScreen.MainScreen.Bounds.Width / 3 - 10, 30);
        _answer1.SizeToFit();
        _answer1.TextAlignment = UITextAlignment.Justified;
        _answer1.LineBreakMode = UILineBreakMode.WordWrap;
        _seperator.Frame = new CGRect(_width + 10, 10, UIScreen.MainScreen.Bounds.Width / 3, 30);
       // _date.Frame = new CGRect(17, 50, 50, 20);
    }
}

我想将一些数据绑定到表中,并且每个项目的长度都不同,根据数据的长度,行高会自动调整大小.我怎样才能做到这一点.

I want to bind some data into the table and the length of the each item is different, according to the length of the data, the row height is automatically resize . How can i do that.

现在桌子看起来像这样

我想要这样的桌子

推荐答案

如果要自动调整tableView的行高大小,则应使用autoLayout将控件放置在单元格中,而不是在Frame中.还将约束代码放在单元格的构造函数中会更好:

If you want to automatically resize the tableView's row height, you should use autoLayout to place your controls in the cell instead of the Frame. Also put the constraints code in the constructor of the cell will be better:

public CaseDetailsTableCell(NSString cellId) : base(UITableViewCellStyle.Default, cellId)
{
    _width = UIScreen.MainScreen.Bounds.Width / 3;

    _qst1 = new UILabel();

    _qst1.Font = _qst1.Font.WithSize(15);

    _seperator = new UILabel();
    _seperator.TextAlignment = UITextAlignment.Center;
    _seperator.Text = ":";
    _seperator.Font = _seperator.Font.WithSize(20);

    _answer1 = new UILabel();

    _answer1.Font = _answer1.Font.WithSize(15);

    ContentView.AddSubviews(new UIView[] { _qst1, _seperator, _answer1 });

    // Disable this to enable autoLayout
    _qst1.TranslatesAutoresizingMaskIntoConstraints = false;

    var qstLeading = NSLayoutConstraint.Create(_qst1, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Leading, 1, 10);
    var qstTop = NSLayoutConstraint.Create(_qst1, NSLayoutAttribute.Top, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Top, 1, 10);
    var qstWidth = NSLayoutConstraint.Create(_qst1, NSLayoutAttribute.Width, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 0, _width + 10);
    var qstBottom = NSLayoutConstraint.Create(_qst1, NSLayoutAttribute.Bottom, NSLayoutRelation.LessThanOrEqual, ContentView, NSLayoutAttribute.Bottom, 1, -10);
    _qst1.Lines = 0;
    _qst1.SizeToFit();
    _qst1.LineBreakMode = UILineBreakMode.Clip;
    _qst1.TextAlignment = UITextAlignment.Left;
    _qst1.BackgroundColor = UIColor.Blue;

    _seperator.TranslatesAutoresizingMaskIntoConstraints = false;

    var sepLeading = NSLayoutConstraint.Create(_seperator, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, _qst1, NSLayoutAttribute.Trailing, 1, 10);
    var sepTop = NSLayoutConstraint.Create(_seperator, NSLayoutAttribute.Top, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Top, 1, 10);
    var sepWidth = NSLayoutConstraint.Create(_seperator, NSLayoutAttribute.Width, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 0, UIScreen.MainScreen.Bounds.Width / 3);
    _seperator.BackgroundColor = UIColor.Yellow;

    _answer1.TranslatesAutoresizingMaskIntoConstraints = false;

    var ansLeading = NSLayoutConstraint.Create(_answer1, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, _seperator, NSLayoutAttribute.Trailing, 1, 10);
    var ansTop = NSLayoutConstraint.Create(_answer1, NSLayoutAttribute.Top, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Top, 1, 10);
    var ansTrailing = NSLayoutConstraint.Create(_answer1, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Trailing, 1, -10);
    var ansBottom = NSLayoutConstraint.Create(_answer1, NSLayoutAttribute.Bottom, NSLayoutRelation.LessThanOrEqual, ContentView, NSLayoutAttribute.Bottom, 1, -10);

    _answer1.SizeToFit();
    _answer1.TextAlignment = UITextAlignment.Justified;
    _answer1.LineBreakMode = UILineBreakMode.WordWrap;
    _answer1.BackgroundColor = UIColor.Red;
    _answer1.Lines = 0;

    ContentView.AddConstraints(new NSLayoutConstraint[] { qstTop, qstLeading, qstWidth, qstBottom, sepLeading, sepTop, sepWidth, ansLeading, ansTop, ansTrailing, ansBottom });
}

在这里,我将LessThanOrEqual NSLayoutRelation添加到_qst1和_answer1标签的底部约束中,该单元将根据最高的高度来调整其高度.您可以调整约束以适合您的要求.

Here I add the LessThanOrEqual NSLayoutRelation to bottom constraint of the _qst1 and _answer1 labels, the cell will adjust its height depending on the tallest one. You can adjust the constraints to fit your requirements.

这篇关于如何在Xamarin iOS中自动调整UITableView的行高大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆