2014-04-07 2 views
0

Xamarin iOS와 함께 응용 프로그램을 만들고 있습니다. Xcode에 UITableView을 넣었습니다. 버튼을 클릭하면 데이터베이스와 슬롯에서 가져옵니다. 행에 넣을 수는 있지만 여러 행을 갖는 방법을 알아낼 수는 없습니다. 데이터가 포함되어 있습니다. 이것은 데이터 행을 표시 할 수있는 부분 코드입니다.Xamarin iOS에서 SQLite에서 여러 행 검색

cmd.CommandType = CommandType.Text; 
dr = cmd.ExecuteReader(); 
while (dr.Read()) 
    { 
     var table = new UITableView(this.retrieveData.Frame); 
     string[] tableItems = new String[] {dr["admin_num"] + ", " + dr["name"]}; 
     table.Source = new TableSource(tableItems); 
     Add (table); 
    } 

답변

1

데이터의 각 행에 대해 완전히 새로운 TableView를 만듭니다. 대신 데이터를 반복하여 표시하려는 모든 데이터가 포함 된 데이터 구조 (목록, 배열 등)를 만든 다음 해당 데이터를 TableView/Source에 전달해야합니다.

public List<DemoClass> getDemoClassList() 
    { 
     List<DemoClass> lstDemoClass; 
     DemoClass objDemoClass; 
     try 
     { 
      String strCommandText; 

      strCommandText = "SELECT * FROM DemoClass "; 

      command = new SqliteCommand(strCommandText, connection); 
      lstDemoClass = new List<DemoClass>(); 
      using (var reader = command.ExecuteReader()) 
      { 
       while (reader.Read()) 
       { 
        objDemoClass = new Homes(false); 
        objDemoClass.ID = Convert.ToInt32(reader[0]); 
        objDemoClass.Name = Convert.ToString(reader[1]); 
        lstDemoClass.Add(objDemoClass); 

       } 
      } 
      return lstDemoClass; 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
     finally 
     { 
      command.Dispose(); 
      command = null; 
      lstDemoClass = null; 
      objDemoClass = null; 
     } 
    } 
      public void BindList() 
      { 
       List<DemoClass> lstDemoClass = new List<DemoClass>(); 
       DemoClass hm = new DemoClass(); 
       lstDemoClass = (List<DemoClass>)hm.getDemoClassList(); 

       TableViewDataSource tdatasource = new TableViewDataSource(this, lstDemoClass); 
       table.Hidden = false; 
       table.DataSource = tdatasource; 
       table.Delegate = new TableViewDelegate(this, table, lstDemoClass); 
       table.ReloadData(); 
      } 

getDemoClassList은() SQLite는 테이블에서 검색된 목록을 제공하며, 나중에 테이블 데이터 소스에 대한 목록을 바인딩 할 수 있습니다 :

cmd.CommandType = CommandType.Text; 
    dr = cmd.ExecuteReader(); 

    // you will need a class mydata with Num and Name properties 
    List<mydata> data = new List<mydata>(); 

    while (dr.Read()) 
    { 
    data.Add(new mydata { Num = dr["admin_num"], Name = dr["name"] }); 
    } 

    dr.Close(); 

    var table = new UITableView(this.retrieveData.Frame); 
    table.Source = new TableSource(data); 
    Add (table); 
+0

조언을 주셔서 감사합니다. num과 name 속성이 무엇인지 알 수 있습니까? – zhihao

+0

코드에 따라 테이블에 표시하려는 데이터 요소가 있다고 가정합니다. 이 클래스는 데이터 모델이 무엇이든 반영해야합니다. – Jason

+0

내가 참조 할 수있는 예제가 있습니까? – zhihao

0

은 무엇 당신이해야 할 것은 이것이다.

업데이트 : 귀하의 요청에 따라 데이터 소스 및 해당 대리인 클래스 용 코드에 대한 의견을 업데이트했습니다. 지금이 같은 클래스에 다음과 같은 서브 클래스를 추가해야합니다

#region TableDelegate 
    public class TableViewDelegate : UITableViewDelegate 
    { 
     private DemoPageViewController _Controller; 
     private List<DemoClass> lst; 

     public TableViewDelegate(DemoPageViewController controller ,UITableView tableView, List<DemoClass> tblList) 
     { 
      try 
      { 
       this._Controller = controller; 
       this.lst = tblList; 
      } 
      catch(Exception ex) 
      { 

      } 
     } 

     public override void RowSelected (UITableView tableView, NSIndexPath indexPath) 
     { 
      try 
      { 
       //This loads the activity spinner till the selection code is completed 
       _Controller._loadPop = new LoadingOverlay (new System.Drawing.RectangleF(0,0,_Controller.View.Frame.Width,_Controller.View.Frame.Height),"Loading..."); 
       _Controller.View.Add (_Controller._loadPop); 

       // spin up a new thread to do some long running work using StartNew 
       Task.Factory.StartNew (
        // tasks allow you to use the lambda syntax to pass work 
        () => { 
        InvokeOnMainThread(delegate{ 

         DemoClass f = lst[indexPath.Row]; 

         //Add your code here, usually some navigation or showing a popup 
        }); 
       }).ContinueWith(t => InvokeOnMainThread(() => { 
        //Hide the activity spinner 
        _Controller._loadPop.Hide(); 
       })); 

      } 
      catch(Exception ex) 
      { 

      } 
      finally  
      { 
      } 
     } 
    } 

    #endregion 

    #region TableDataSource 

    private class TableViewDataSource : UITableViewDataSource 
    { 
     static NSString kCellIdentifier = new NSString("MyIdentifier"); 
     private List<DemoClass> lst; 
     private DemoPageViewController controller; 

     public TableViewDataSource (DemoPageViewController controller ,List<DemoClass> tblLst) 
     { 
      this.controller = controller; 
      this.lst = tblLst; 
     } 

     public override int NumberOfSections (UITableView tableView) 
     { 
      return 1; 
     } 

     public override int RowsInSection (UITableView tableView, int section) 
     { 
      return lst.Count; 
     } 

     // Override to support conditional editing of the table view. 
     public override bool CanEditRow (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath) 
     { 
      // Return false if you do not want the specified item to be editable. 
      return false; 
     } 


     public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath) 
     { 
      try 
      { 
       UITableViewCell cell = tableView.DequeueReusableCell (kCellIdentifier); 
       if (cell == null) 
       { 
        cell = new UITableViewCell (UITableViewCellStyle.Subtitle, kCellIdentifier); 
        cell.Tag = Environment.TickCount; 
       } 

       DemoClass objDemo = lst[indexPath.Row]; 
       cell.Accessory = UITableViewCellAccessory.DisclosureIndicator; 

       cell.ImageView.Image = UIImage.FromFile("Images/CameraImg.png"); 
       cell.DetailTextLabel.Text = "Show some detail: " + objDemo.DemoDescription.ToString(); 
       cell.TextLabel.Text = "Some Title: " + objDemo.DemoTitle.ToString(); 
       return cell; 
      } 
      catch(Exception ex) 
      { 
       return null; 
      } 
      finally  
      { 

      } 
     } 
    } 

    #endregion 

는 도움이되기를 바랍니다.

+0

게시 할 수 있으면 감사하겠습니다. =)) – zhihao