2016-07-25 69 views
0

我正試圖創建一個屏幕,像iOS中的Android屏幕 enter image description here如何更改UiTableViewCell中的ImageView?

我的問題,在iOS中是圓圈。在iOS中,我將ImageView用於具有兩個png文件的圓圈:red_circle和green_circle。我需要使用紅色圓圈表示正值,綠色圓圈表示負值,但我不知道如何在代碼中檢查此情況。誰能幫我?我用MvvmCross使用Xamarin.iOS。

這是我的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]; 
    }  


} 
+0

你的圈子的ImageView被定義在哪裏?也許你可以創建一個轉換器,它根據你的ViewModel的正值/負值來控制你的ImageView控件並綁定一個UIImage。 – Plac3Hold3r

+0

我在XIB文件中定義了ImageView。我有兩個ImageViews的圈子和我的iOS項目中包含兩個PNG文件的文件夾:red_circle和green_circle。 –

+0

你的MvxTableViewCell中有你的'ImageView'的上下文嗎? – Plac3Hold3r

回答

3

我認爲這是最簡單的解決辦法是使用一個轉換器做了圖像的切換基於該值的顏色,即如果正值=紅色png和負值/中性(0)=綠色png。

public class CostColorConverter : MvxValueConverter<double, UIImage> 
{ 
    protected override UIImage Convert(double value, Type targetType, object parameter, CultureInfo culture) 
    { 
     if (value > 0) 
      return UIImage.FromBundle("images/red_circle .png"); 

     return UIImage.FromBundle("images/green_circle.png"); 
    } 
} 

您可能需要更新FromBundle地址以匹配您的圖像存儲位置。 然後添加綁定以根據ViewModel中的值更新圖像。 喜歡的東西:

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(); 

注意,我個人比較喜歡使用強類型轉換器名稱的做法,但你也可以只使用轉換器的字符串名字就像你在你的問題都有。

+0

謝謝你的男人!奇蹟般有效。 –