基于uilabel的动态uitableviewcell

发布于 2025-02-12 11:41:58 字数 1974 浏览 0 评论 0原文

与Xamarin.ios一起工作,我需要我的UITATION ViewCell根据Uilabel的大小(文本数)来调整其高度。我有一个自定义的UitaiteViewCell,可以在其中创建Uilabel,并且基于用户输入设置文本。我真正知道如何使用public Override Nint RowsInsection(Uaithitview TableView,NINT部分) method方法,我真正知道如何在UITATIONVIEWSOURCE类中调整单元格的高度。

    public class ChatTableSource : UITableViewSource
    {
        public List<MessageDetails> messages;

        public ChatTableSource()
        {

        }

        public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
        {
            CustomMessageCell cell = tableView.DequeueReusableCell(cellIdentifier) as CustomMessageCell;

            // if there are no cells to reuse, create a new one
            if (cell == null)
                cell = new CustomMessageCell(cellIdentifier);

            tableView.Layer.BackgroundColor = UIColor.White.CGColor;

            cell.UpdateCell(messages[indexPath.Row].body);

            return cell;
        }

        public override nint RowsInSection(UITableView tableview, nint section)
        {
            return messages.ToArray().Length;
        }

        //public override nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
        //{
        //    return dynamic size
        //}

    }
    public class CustomMessageCell : UITableViewCell
    {

        UILabel body;

        public CustomMessageCell(NSString cellId) : base(UITableViewCellStyle.Default, cellId)
        {

            body = new UILabel();
        
            ContentView.Add(body);
        }

        public void UpdateCell(string body)
        {
            this.body.Text = body;
        }



        public override void LayoutSubviews()
        {
            base.LayoutSubviews();

            body.Frame = new CGRect(distanceFromScreenEdge, ContentView.Bounds.Top, longTextSize, ContentView.Bounds.Height);
                
            body.Lines = 0;
        }
    }


Working with Xamarin.ios and I need my UITableViewCell to adjust its height based on the size (number of lines of text) of a UILabel. I have a custom UITableViewCell where the UILabel is created and the text is set based on user input. The only way I really know how to adjust the height of the cell is in the UITableViewSource class using the public override nint RowsInSection(UITableView tableview, nint section) method.

    public class ChatTableSource : UITableViewSource
    {
        public List<MessageDetails> messages;

        public ChatTableSource()
        {

        }

        public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
        {
            CustomMessageCell cell = tableView.DequeueReusableCell(cellIdentifier) as CustomMessageCell;

            // if there are no cells to reuse, create a new one
            if (cell == null)
                cell = new CustomMessageCell(cellIdentifier);

            tableView.Layer.BackgroundColor = UIColor.White.CGColor;

            cell.UpdateCell(messages[indexPath.Row].body);

            return cell;
        }

        public override nint RowsInSection(UITableView tableview, nint section)
        {
            return messages.ToArray().Length;
        }

        //public override nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
        //{
        //    return dynamic size
        //}

    }
    public class CustomMessageCell : UITableViewCell
    {

        UILabel body;

        public CustomMessageCell(NSString cellId) : base(UITableViewCellStyle.Default, cellId)
        {

            body = new UILabel();
        
            ContentView.Add(body);
        }

        public void UpdateCell(string body)
        {
            this.body.Text = body;
        }



        public override void LayoutSubviews()
        {
            base.LayoutSubviews();

            body.Frame = new CGRect(distanceFromScreenEdge, ContentView.Bounds.Top, longTextSize, ContentView.Bounds.Height);
                
            body.Lines = 0;
        }
    }


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画▽骨i 2025-02-19 11:41:58

我不使用Xamarin - 所以我无法测试 - 但这应该是您想做的...

使用约束,让自动layout为您处理尺寸。

public class CustomMessageCell : UITableViewCell
{

    UILabel body;

    public CustomMessageCell(NSString cellId) : base(UITableViewCellStyle.Default, cellId)
    {

        body = new UILabel();
    
        ContentView.Add(body);

        var margins = View.LayoutMarginsGuide;

        // Pin the leading edge of body to the margin
        body.LeadingAnchor.ConstraintEqualTo (margins.LeadingAnchor).Active = true;

        // Pin the trailing edge of body to the margin
        body.TrailingAnchor.ConstraintEqualTo (margins.TrailingAnchor).Active = true;

        // Pin the top edge of body to the margin
        body.TopAnchor.ConstraintEqualTo (margins.TopAnchor).Active = true;

        // Pin the bottom edge of body to the margin
        body.BottomAnchor.ConstraintEqualTo (margins.BottomAnchor).Active = true;

        // set body number of lines to Zero so it will automatically set its own height
        body.Lines = 0

    }

    public void UpdateCell(string body)
    {
        this.body.Text = body;
    }

}

您根本不需要layoutsubviews()

I don't work with Xamarin - so I can't test this - but this should be what you want to do...

Use constraints and let auto-layout handle the sizing for you.

public class CustomMessageCell : UITableViewCell
{

    UILabel body;

    public CustomMessageCell(NSString cellId) : base(UITableViewCellStyle.Default, cellId)
    {

        body = new UILabel();
    
        ContentView.Add(body);

        var margins = View.LayoutMarginsGuide;

        // Pin the leading edge of body to the margin
        body.LeadingAnchor.ConstraintEqualTo (margins.LeadingAnchor).Active = true;

        // Pin the trailing edge of body to the margin
        body.TrailingAnchor.ConstraintEqualTo (margins.TrailingAnchor).Active = true;

        // Pin the top edge of body to the margin
        body.TopAnchor.ConstraintEqualTo (margins.TopAnchor).Active = true;

        // Pin the bottom edge of body to the margin
        body.BottomAnchor.ConstraintEqualTo (margins.BottomAnchor).Active = true;

        // set body number of lines to Zero so it will automatically set its own height
        body.Lines = 0

    }

    public void UpdateCell(string body)
    {
        this.body.Text = body;
    }

}

You don't need LayoutSubviews() at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文