Xamarin iOSのデータを取り込んでTableView
を作成しています。私が使用するコードは以下の通りです。Xamarin ios Appのヌルリファレンス
コード:
public partial class SearchViewController : UIViewController
{
private SearchFilter searchFilter;
List<SearchItem> searchItem;
SearchTableSourceClass searchTablesourceclass;
UITableView searchTableView;
static NSString TextCellId = new NSString("TextCell");
int Skip = 0, Take = 10;
public SearchViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
this.Title = "Search";
this.NavigationController.NavigationBar.TitleTextAttributes = new UIStringAttributes() { ForegroundColor = UIColor.White };
this.NavigationController.NavigationBar.BarTintColor = UIColor.Clear.FromHexString("#0072BA", 1.0f);
this.NavigationController.NavigationBarHidden = false;
btn_filter.TintColor = UIColor.White;
searchTableView = new UITableView
{
Frame = new CoreGraphics.CGRect(0, 0, View.Bounds.Width, View.Bounds.Height)
};
View.AddSubview(searchTableView);
FnBindData();
}
async void FnBindData()
{
searchFilter = new SearchFilter(); ;
searchFilter.Speciality = "Physician";
searchFilter.Speciality = "";
searchFilter.Days = new List<string>();
searchFilter.Days.Add("Monday");
searchFilter.Days.Add("TuesDay");
searchFilter.Days.Add("Wednesday");
searchFilter.Days.Add("Thursday");
searchFilter.Days.Add("Friday");
searchFilter.Days.Add("Saturday");
searchFilter.Days.Add("Sunday");
searchFilter.Area = "";
searchFilter.City = "Surat";
searchFilter.MaxFees = 500.0;
searchFilter.FromTime = 570;
searchFilter.ToTime = 750;
searchItem = new List<SearchItem>();
var json = JsonConvert.SerializeObject(searchFilter);
searchItem = await SearchServices.GetSearchItem(Take, Skip, json);
if (searchItem != null)
{
searchTablesourceclass = new SearchTableSourceClass(searchItem);
searchTableView.RegisterClassForCellReuse(typeof(TextCell),TextCell.CellId);
searchTableView.Source = searchTablesourceclass;
}
}
}
class SearchTableSourceClass : UITableViewSource
{
List<SearchItem> searchItem;
public SearchTableSourceClass(List<SearchItem> searchItems)
{
this.searchItem = searchItems;
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
var cell = (TextCell)tableView.DequeueReusableCell(TextCell.CellId, indexPath);
var item = searchItem[indexPath.Row];
if (cell == null)
{
cell = (Gargi.iOS.TextCell)new UITableViewCell(UITableViewCellStyle.Default, TextCell.CellId);
}
cell.Hospital = item.Organization;
return cell;
}
public override nint RowsInSection(UITableView tableview, nint section)
{
return searchItem.Count;
}
}
class TextCell : UITableViewCell
{
UILabel hospitalLabel;
public static readonly NSString CellId = new NSString("TextCell");
public TextCell(IntPtr ptr) : base(ptr)
{
}
public string Hospital
{
get
{
return hospitalLabel.Text;
}
set
{
hospitalLabel.Text = value;
hospitalLabel.TextColor = UIColor.Black;
SetNeedsDisplay();
}
}
[Export("initWithFrame:")]
TextCell(RectangleF frame) : base(frame)
{
hospitalLabel = new UILabel()
{
Frame = new CoreGraphics.CGRect(0, 0, ContentView.Frame.Size.Width - 70, 20),
TextColor = UIColor.Black,
AdjustsFontSizeToFitWidth = true,
Font = UIFont.FromName("Helvetica-Bold", 14f),
TextAlignment = UITextAlignment.Left
};
ContentView.AddSubview(hospitalLabel);
}
}
しかし、このセクションでは、デバッグのApp
set
{
hospitalLabel.Text = value;
hospitalLabel.TextColor = UIColor.Black;
SetNeedsDisplay();
}
それはエラーの下に与えながら:
System.NullReferenceException:オブジェクト参照に設定されていませんがGargi.iOS.TextCell.setのオブジェクトのインスタンス _Hospital(可能System.String 値)[0x00008] Gargi.iOS.SearchTableSourceClass.GetCellで /Users/BizSalt/Projects/Gargi/Gargi.iOS/SearchViewController.cs:131
(UIKit.UITableView のtableView、財団です。 UIApplicationMain(int型、文字列[]:(ラッパーは管理ツーネイティブ) UIKit.UIApplicationでの /Users/BizSalt/Projects/Gargi/Gargi.iOS/SearchViewController.cs:102
でNSIndexPath indexPath)[0x00046] 、intptr、intptr)
UIKit.UIApplication.Main(System.String [] args、System.IntPtr プリンシパル、System.IntPtrデリゲート)[0x00005] /Users/builder/data/lanes/3969/44931ae8/source/xamarin-macios/src/UIKit/UIApp/Users/builder/data/lanes/3969/44931ae8/source内の[0x00038] [] [] [] [] /xamarin-macios/src/UIKit/UIApplication.cs:63 Gargi.iOS.Application.Main(System.String [] args)[0x00008] /Users/BizSalt/Projects/Gargi/Gargi.iOS/Main .cs:12
たぶん 'item.Organization'はnullですか? – Nerkyator
@Nerkyatorいいえ、それは 'value'を持っています。 「ヌル」ではない。 – Ironman