【问题标题】:Circular imageview initially load as square in UITableViewCell - Xamarin.iOS圆形图像视图最初在 UITableViewCell 中加载为正方形 - Xamarin.iOS
【发布时间】:2019-06-03 10:40:12
【问题描述】:

我将我的UIImageView 设为我的UITableViewCell 中的圈子。所以我在GetCell 方法中做了这样的事情,如下所示。

public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        UITableViewCell cell = tableView.DequeueReusableCell(_cellIdentifier);
        RemotesupportAgentData item = _tableItems[indexPath.Row];
        //---- if there are no cells to reuse, create a new one
        if (cell == null)
        {
            cell = new UITableViewCell(UITableViewCellStyle.Default, _cellIdentifier);
            cell.SelectionStyle = UITableViewCellSelectionStyle.None;
        }

        cell.TextLabel.Text = (string)item.Attributes["Name"];

        cell.ImageView.SetImage(new NSUrl((string)item.Attributes["avatar"]), UIImage.FromBundle("contacts-32.png"));
        cell.ImageView.ClipsToBounds = true;
        cell.ImageView.Layer.CornerRadius = cell.ImageView.Frame.Width / 2;
        cell.ImageView.Layer.BorderColor = UIColor.Green.CGColor;
        cell.ImageView.Layer.BorderWidth = (nfloat)2.0;

        return cell;
    }

我的问题是,最初,当我滚动时,这个图像视图加载为正方形,只有它自己变成圆形。我该如何解决这个问题?

【问题讨论】:

    标签: uitableview xamarin.ios uiimageview


    【解决方案1】:

    这是因为cell.ImageView的帧在初始时间为零。在完成显示之前,它不会呈现为实际大小。

    而且我不建议你将配置代码放在单元格的重用方法中。当单元格出现时,将一遍又一遍地调用此事件。出于性能考虑,我们应该只将设置值的东西放在那个事件中。

    首先,创建一个新的表格视图类并在那里进行一些配置:

    public class MyCustomCell : UITableViewCell
    {
        public MyCustomCell(IntPtr handle) : base(handle)
        {
            SelectionStyle = UITableViewCellSelectionStyle.None;
    
            ImageView.ClipsToBounds = true;   
            ImageView.Layer.BorderColor = UIColor.Green.CGColor;
            ImageView.Layer.BorderWidth = (nfloat)2.0;
        }
    
        public override void Draw(CGRect rect)
        {
            base.Draw(rect);
    
            // Your will get the actual size in the event
            if (ImageView.Layer.CornerRadius == 0)
                ImageView.Layer.CornerRadius = ImageView.Frame.Width / 2;
        }
    }
    

    然后,在初始化表格视图时注册它:

    tableView.Source = new MyTableViewSource(list);
    tableView.RegisterClassForCellReuse(typeof(MyCustomCell), _cellIdentifier);
    

    最后,你的 get cell 方法可以是这样的:

    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        MyCustomCell cell = tableView.DequeueReusableCell(_cellIdentifier) as MyCustomCell;
        RemotesupportAgentData item = _tableItems[indexPath.Row];
    
        cell.TextLabel.Text = (string)item.Attributes["Name"];
    
        cell.ImageView.SetImage(new NSUrl((string)item.Attributes["avatar"]), UIImage.FromBundle("contacts-32.png"));
    
        return cell;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-26
      • 2021-03-17
      • 2015-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-08
      相关资源
      最近更新 更多