【问题标题】:xamarin.iOS Vertically scroll through Imagesxamarin.iOS 垂直滚动图像
【发布时间】:2016-08-10 14:24:00
【问题描述】:

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

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

或者更好的方法是添加一个 ScrollView 并向其中添加图像,允许用户向下滚动滚动视图?

任何建议或代码示例将不胜感激。

【问题讨论】:

    标签: ios uitableview uiscrollview xamarin.ios


    【解决方案1】:

    你可以使用 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;
        }
    }
    

    希望对你有帮助。

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

    【讨论】:

    • Alnc 这是您第二次为我的一个问题提供出色的解决方案。通过出色的代码示例清晰简洁的答案。我爱你的工作,谢谢你
    • 欢迎您,希望您喜欢使用 Xamarin 进行开发。
    猜你喜欢
    • 1970-01-01
    • 2014-10-13
    • 2017-06-23
    • 2017-07-30
    • 2016-02-16
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    相关资源
    最近更新 更多