2017-10-04 21 views
1

私は、ユーザーがクライアント情報を入力する必要があるアプリケーションを作成しています。この情報は次のページに送られ、テーブル形式で表示されます。値を挿入することはできますが、テーブル形式で再検索することはできません。助けてください。Xamarin IOS SQLite

private string pathtoDatabase; 
    public RoofExisting(IntPtr handle) : base(handle) 
    { 
     var documentFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal); 
     pathtoDatabase = Path.Combine(documentFolder, "ClientInfo_db.db"); 

     using (var connection = new SQLiteConnection(pathtoDatabase)) 
     { 
      var con = connection.Table<ClientInfo>(); 
      foreach (var item in con) 
      { 
       ClientInfo Info = new ClientInfo(item.projectno, item.ClientName, item.CCompany, item.Caddress, item.CPostal, item.CPhonen, item.CEmail); 

       //Adding in the table view 
       CNinfo.Source = new TableSource(Info, this); 
      } 
     } 
    } 

class TableSource:UITableViewSource 
{ 
    protected string[] tableItems; 
    protected String cellidentifier = "TableCell"; 
    ViewController owner; 
    private ClientInfo info; 
    private RoofExisting roofExisting; 

    public TableSource(String[] items, ViewController owner) 
    { 
     tableItems = items; 
     this.owner = owner; 
    } 

    public TableSource(ClientInfo info, RoofExisting roofExisting) 
    { 
     this.info = info; 
     this.roofExisting = roofExisting; 
    } 

    public override nint RowsInSection(UITableView tableview, nint section) 
    { 
     return tableItems.Length; 
    } 
    public override void RowSelected(UITableView tableView, NSIndexPath indexPath) 
    { 
     UIAlertController alertcontroller = UIAlertController.Create("Row Selected", tableItems[indexPath.Row], UIAlertControllerStyle.Alert); 
     alertcontroller.AddAction(UIAlertAction.Create("ok",UIAlertActionStyle.Default,null)); 
     owner.PresentViewController(alertcontroller,true,null); 

     tableView.DeselectRow(indexPath,true); 
    } 
    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) 
    { 
     UITableViewCell cell = tableView.DequeueReusableCell(cellidentifier); 
     if (cell == null) 
      cell = new UITableViewCell(UITableViewCellStyle.Default, cellidentifier); 

     cell.TextLabel.Text = tableItems[indexPath.Row]; 
     return cell;   
    } 
} 

答えて

0

私は1つのエラーは、あなたがforeachの内側(つまり、何でも)単一CNInfoを設定していることであると仮定 - それはあなたがそれを毎回上書きされ、最後のものだけが動作することを意味します。

私はこの行を(少なくとも)リファクタリングになります。

CNinfo.Source = new TableSource(Info, this); 

はまた、あなたは完全に異なることを行う2つのコンストラクタを持っています。 public TableSource(ClientInfo info, RoofExisting roofExisting)を呼び出すと、tableItemsownerはnullになり、他のコンストラクタでのみ設定されるため、オーバーライドメソッドでNullReferenceExceptionがスローされます。

私はしばらくUITableViewSourceと混乱していないが、私ははそれが項目のコレクションを開催するを思うだろう。それぞれUITableViewSourceContactInfoとしています。

これらの2つのクラスをリファクタリングする必要があります。このコードを変更しようとする前にUITableViewSourceを作成する例をいくつか見てみてください。そうすれば、このコードを実装する方法がわかります。

関連する問題