xamarin.iOS垂直滚动图像

时间:2016-08-10 14:24:00

标签: ios uitableview uiscrollview xamarin.ios

我正在尝试在Xamarin.iOS中构建一个UI,允许用户垂直滚动图像列表。我不确定最好的方法是什么。我已经看过使用UITableViewSource但是我不确定这是否是最好的方法。

我想要显示的数据不是真正的表格,它只是一个图像列表。有没有办法模塑UITableViewSource只显示一个与屏幕一样宽的图像,允许用户向下滚动它们。

或者更好的方法是添加ScrollView并添加图像,以便用户只需向下滚动滚动视图?

非常感谢任何建议或代码示例。

1 个答案:

答案 0 :(得分:2)

您可以使用UITableView来实现您的需求,这是一个示例。

首先,你可以像这样调用示例代码:

public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();
        // Perform any additional setup after loading the view, typically from a nib.

        List<string> dataList = new List<string> ();
        for (int i = 0; i < 20; i++) {
            //Make sure all images is already in the Resources folder
            dataList.Add ("test.png");
        }

        UITableView myTalbeView = new UITableView ();
        myTalbeView.RowHeight = 80;
        myTalbeView.Frame = UIScreen.MainScreen.Bounds;
        myTalbeView.AllowsSelection = false;//If you don't want any image be selected, use it.
        myTalbeView.SeparatorStyle = UITableViewCellSeparatorStyle.None;//remove the under line
        myTalbeView.Source = new MyTableViewSource (dataList);//set source for tableView
        this.View.AddSubview (myTalbeView);
    }

这是MyTableViewSource.cs:

public class MyTableViewSource : UITableViewSource
{
    private static string cellReuseID = "MyTalbeCell";
    private List<string> dataList;

    public MyTableViewSource(List<string> _dataList)
    {
        this.dataList = _dataList;
    }

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

    public override UITableViewCell GetCell (UITableView tableView, Foundation.NSIndexPath indexPath)
    {
        MyTableCell cell = tableView.DequeueReusableCell (cellReuseID) as MyTableCell;
        if (null == cell)
            cell = new MyTableCell (UITableViewCellStyle.Default, cellReuseID);
        cell.ImageName = dataList [indexPath.Row];
        return cell;
    }
}

这是MyTalbeCell.cs:

public class MyTableCell : UITableViewCell
{
    private UIImageView imageView;

    private string imageName = "";
    public string ImageName{
        get{ 
            return imageName;
        }
        set{ 
            imageName = value;
            imageView.Image = UIImage.FromFile (imageName);
        }
    }

    public MyTableCell (UITableViewCellStyle style,string reuseID) : base(style,reuseID)
    {
        imageView = new UIImageView ();
        this.AddSubview (imageView);
    }

    public override void LayoutSubviews ()
    {
        imageView.Frame = this.Bounds;
    }
}

希望它可以帮到你。

如果您仍然需要帮助,请在此处留下问题,我会检查后者。