如何在UiTableViewCell中以动态方式更改ImageView?

时间:2016-07-25 17:35:59

标签: ios uitableview xamarin xamarin.ios mvvmcross

我正在尝试在iOS下创建一个类似Android屏幕的屏幕 enter image description here

我的问题,在iOS中就是圈子。在iOS中,我使用ImageView作为带有两个png文件的圆圈:red_circle和green_circle。我需要使用红色圆圈表示正值,使用绿色圆圈表示负值,但我不知道如何在代码中检查这个条件。谁能帮我?我正在使用Xamarin.iOS和MvvmCross。

这是我的UIViewController:

public partial class MyProjectsView : MvxViewController<MyProjectsViewModel>
{
    public MyProjectsView() : base("MyProjectsView", null)
    {
    }

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

        // Release any cached data, images, etc that aren't in use.
    }

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


        if (RespondsToSelector(new Selector("edgesForExtendedLayout")))
        {
            EdgesForExtendedLayout = UIRectEdge.None;
        }

        var source = new MvxSimpleTableViewSource(TblProjects, MyProjectsItem.Key, MyProjectsItem.Key);
        TblProjects.Source = source;



        this.CreateBinding(source).To<MyProjectsViewModel>(viewModel => viewModel.Projetos).Apply();
        this.CreateBinding(LblSyncrhonizingData).For("Visibility").To<MyProjectsViewModel>(vm => vm.IsProgressBarVisible).WithConversion("Visibility").Apply();
        this.CreateBinding(Activity).For("Visibility").To<MyProjectsViewModel>(vm => vm.IsProgressBarVisible).WithConversion("Visibility").Apply();
        this.CreateBinding(LblDataSynchronized).For("Visibility").To<MyProjectsViewModel>(vm => vm.IsDataSynchronized).WithConversion("Visibility").Apply();

        var bounds = UIScreen.MainScreen.Bounds;
        var carregamento = new CarregamentoIOS(bounds);
        View.Add(carregamento);

        ViewModel.Carregamento = carregamento;
        await ViewModel.PreencheLista();
    }


}

这是我的UITableViewCell:

public partial class MyProjectsItem : MvxTableViewCell
{
    public static readonly NSString Key = new NSString("MyProjectsItem");
    public static readonly UINib Nib = UINib.FromName("MyProjectsItem", NSBundle.MainBundle);

    protected MyProjectsItem(IntPtr handle) : base(handle)
    {
        this.DelayBind(() =>
        {
            this.CreateBinding(LblProjectName).To<Project>(project => project.Name).Apply();
            this.CreateBinding(LblPercentage).To<Project>(project => project.PercentCompleted).WithConversion("IntToPercentCompleted").Apply();
            this.CreateBinding(LblCostMoney).To<Project>(project => project.Cost.Variation).WithConversion("DoubleThousandToStringAbbreviation").Apply();
            this.CreateBinding(LblCostDays).To<Project>(project => project.Schedule.Variation).WithConversion("IntToDaysAbbreviation").Apply();
        });          
    }

    public static MyProjectsItem Create()
    {
        return (MyProjectsItem)Nib.Instantiate(null, null)[0];
    }      


}

1 个答案:

答案 0 :(得分:3)

我认为对此最简单的解决方案是利用转换器根据值切换图像颜色,即如果正=红色png和负/中性(0)=绿色png。

FromBundle

您可能需要更新this.CreateBinding(ImgViewCostMoney) .For(c => c.Image) .To<Project>(project => project.Cost.Variation) .WithConversion(new CostColorConverter()) .Apply(); this.CreateBinding(ImgViewCostDays) .For(c => c.Image) .To<Project>(project => project.Schedule.Variation) .WithConversion(new CostColorConverter()) .Apply(); 地址以匹配图像的存储位置。 然后添加绑定以根据ViewModel中的值更新图像。 类似的东西:

create proc insert_Orders_sp
    @Uname varchar(10) -- Missing Datatype
as
    insert into Orders(Uid, Quantity)
    values((select Users.Uid from Users where Users.Uname = @Uname), 15)

注意,我个人更喜欢使用强类型转换器名称方法,但您也可以将转换器名称用作您在问题中的字符串。