c# - 如何在 xamarin ios 中创建 ListView

标签 c# listview xamarin.ios tableview

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

请帮帮我...

这是我的代码

    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 方法中遇到异常

我想要这个设计用于我的屏幕 enter image description here

最佳答案

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

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

旅游 here .

样本 here .

关于c# - 如何在 xamarin ios 中创建 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50465844/

相关文章:

ios - 如何使分组的 ListView 标题在 iOS 中正确 float (不粘)?

python - PyGTK 如何将 "load-on-demand"数据传到 TreeView

ios - 主 VC 中的工具栏和 Modal VC 中的工具栏不会显示相同的按钮

xamarin.ios - 单点触控 : Right manner for using NSNotificationCenter

c# - 从 MVC Controller 返回 Json 但日期格式在 javascript 中不正确

c# - 如何根据日期时间比较两个文件?

c# - 数据库优先方法的 EF 迁移?

c# - 提高 SQL Server 表的查询性能包含 350 万行并且还在增长

c# - 以编程方式更改 ListView 行的背景颜色(wpf)

c# - 使用 MonoTouch.Dialog 时更改 TabBarController 和 NavigationController