首页 > 解决方案 > 如何在 xamarin ios 中创建列表视图

问题描述

我想创建一个包含多个字段和图像的列表视图。我使用表格视图来开发列表视图,但我不会得到正确的设计。

请帮我...

这是我的代码

    class TableViewSource : UITableViewSource
     {
    List<Sample> tabledata;

    public TableViewSource(List<Sample> items)
    {
        tabledata = items;
    }

    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        var cell = tableView.DequeueReusableCell("cell") as CustomVegeCell;
        if (cell == null)
        cell.UpdateCell(tabledata[indexPath.Row].CaseID
                , tabledata[indexPath.Row].Description
                , UIImage.FromFile("right_arrow.png"));
        return cell;
    }

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

    public override void WillDisplay(UITableView tableView, UITableViewCell cell, NSIndexPath indexPath)
    {

        if (indexPath.Row == tabledata.Count - 1)
        {
            //Reload your data here
        }
    }
}

这是我的 ViewController.cs

    List<Sample> _itemdata;
    public ViewController(IntPtr handle) : base(handle)
    {
    }

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

        DAtaBinding();
        mainTable.Source = new TableViewSource(_itemdata);
    }

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

    }

    public void DAtaBinding()
    {
        _itemdata = new List<Sample>() { { new Sample { CaseID="CQ964C",Description="Aswathy"} },
                                         { new Sample { CaseID="CQ964C",Description="Anu"}},
                                         { new Sample { CaseID="CQ964C",Description="Anu"}},
                                         { new Sample { CaseID="CQ964C",Description="Anu"}},

                                         { new Sample { CaseID="CQ964C",Description="Anu"}},
                                         { new Sample { CaseID="CQ964C",Description="Anu"}},

                              };
    }
}

这是我的自定义 tableviewcell

CustomVegeCell.cs

   public class CustomVegeCell : UITableViewCell
{
    UILabel headingLabel, subheadingLabel;
    UIImageView imageView;
    public CustomVegeCell(NSString cellId) : base(UITableViewCellStyle.Default, cellId)
    {
        SelectionStyle = UITableViewCellSelectionStyle.Gray;
        ContentView.BackgroundColor = UIColor.FromRGB(218, 255, 127);
        imageView = new UIImageView();
        headingLabel = new UILabel()
        {
            Font = UIFont.FromName("Cochin-BoldItalic", 22f),
            TextColor = UIColor.FromRGB(127, 51, 0),
            BackgroundColor = UIColor.Clear
        };
        subheadingLabel = new UILabel()
        {
            Font = UIFont.FromName("AmericanTypewriter", 12f),
            TextColor = UIColor.FromRGB(38, 127, 0),
            TextAlignment = UITextAlignment.Center,
            BackgroundColor = UIColor.Clear
        };
        ContentView.AddSubviews(new UIView[] { headingLabel, subheadingLabel, imageView });

    }
    public void UpdateCell(string caption, string subtitle, UIImage image)
    {
        imageView.Image = image;
        headingLabel.Text = caption;
        subheadingLabel.Text = subtitle;
    }
    public override void LayoutSubviews()
    {
        base.LayoutSubviews();
        imageView.Frame = new CGRect(ContentView.Bounds.Width - 63, 5, 33, 33);
        headingLabel.Frame = new CGRect(5, 4, ContentView.Bounds.Width - 63, 25);
        subheadingLabel.Frame = new CGRect(100, 18, 100, 20);
    }
}

添加自定义 tableviewcell 后,我在 GetCell 方法中遇到异常

我想要这个设计用于我的屏幕 在此处输入图像描述

标签: c#listviewxamarin.iostableview

解决方案


您必须新建一个继承自的类UITableViewCell,并在其中创建自定义单元格布局。

在这种情况下,您需要三个标签和一个箭头图像,并按照演示调整它们的框架。

这里是Toturial 。

样品在这里


推荐阅读