如何在 xamarin ios 中自动调整 UITableView 的行高

阿斯瓦西

我是 xamarin.ios 新手,我想根据内容长度调整行高。我怎样才能做到这一点。

这是我的 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

   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

  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);
    }
}

我想将一些数据绑定到表格中,每个项目的长度不同,根据数据的长度,行高自动调整大小。我怎样才能做到这一点。在此处输入图片说明

现在桌子看起来像这样

在此处输入图片说明

我想要这样的桌子

匿名的

如果你想自动调整 tableView 的行高,你应该使用 autoLayout 将你的控件放在单元格而不是框架中。也把约束代码放在单元格的构造函数中会更好:

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 });
}

在这里,我将LessThanOrEqualNSLayoutRelation添加到 _qst1 和 _answer1 标签的底部约束,单元格将根据最高的来调整其高度。您可以调整约束以满足您的要求。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

iOS 13:如何调整UIKit / SwiftUI中自定义字体的前导/下降/行高

如何在Xamarin.iOS中清理UIViewController?

自动调整Xamarin Forms中iOS列表行的大小

如何在Xamarin中调整按钮的大小

如何在iOS UITableView中处理动态的节和行

如何在iOS 11中通过UITableView关闭调整大标题?

如何自动调整ScrollView的大小以适合xamarin Forms中的内容

如何在Bootstrap中自动调整行高?

如何在Xamarin iOS中绘制文本?

UITableview自动调整行问题Swift IOS?

UITableView在iOS 11中删除行

如何自动调整表格行高?

如何在UITableView iOS中获取ViewWithTag的UIButton的索引

如何在MS Word中自动调整表格的行高?

如何在iOS的UITableView中添加UIProgressView

如何在没有动画的情况下从UITableView删除行而不在iOS中

如何在iOS中的UITableView中显示UIImage

如何在iOS中向UITableView的最后一行添加带字符串的一行?

iOS在UITableView中插入一行

在uitableview中隐藏行-iOS8

如何在iOS8的UIAlertController中添加UITableview?

如何从xamarin.ios uitableview将数据发送回主页?

iOS-从UITableView崩溃中删除行

如何在Objective C中调整UITableView行的高度

如何从UITableView Xamarin.ios C#中选择行的文本

重用单元如何在iOS的UITableView中工作?

在动态UITableView原型中访问行高

iOS:使用自动调整大小调整 UITableview 中的单元格

如何在 Swift/iOS 中从 UITableView 删除行并从 UserDefaults 更新数组