Xamarin IOS SQLite
问题描述:
我正在创建一个应用程序,用户需要输入客户端信息,这些信息将发送到下一页,并以表格格式显示。我可以插入这些值,但无法以表格格式进行检索。请帮忙。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;
}
}
答
我以为一个错误是,你要设置一个CNInfo
(无论是)一个foreach内 - 这意味着每次只有最后一个会工作要覆盖它。
我会重构这个线(至少):
CNinfo.Source = new TableSource(Info, this);
而且,你有两个构造函数做完全不同的事情。调用public TableSource(ClientInfo info, RoofExisting roofExisting)
意味着tableItems
和owner
将为空,并在您的覆盖方法中抛出NullReferenceException
,因为它们只在其他构造函数中设置。
我还没有在一段时间与UITableViewSource
搞砸,但我会认为,它将持有项目的集合。您正在为制作UITableViewSource
,每个ContactInfo
。
这两个类需要重构。在尝试更改此代码之前,可能会先查看几个有关创建UITableViewSource
的示例,以便您更好地了解如何实现它。