我正在使用monotouch和mailkit库。我正在从pop3邮件帐户中检索电子邮件并将其呈现在tableview上。我想在我的tableview上创建3个部分。第1节“今天”,显示我今天收到的电子邮件。第2节“昨天”,此部分显示我昨天收到的电子邮件。第3节“较旧的电子邮件”,此部分显示我在今天前2天收到的电子邮件。这可能吗?这是我的代码,直到现在:
public class TableSource : UITableViewSource {
protected string[] tableItems;
protected string cellIdentifier = "TableCell";
public TableSource (string[] items)
{
tableItems = items;
}
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
// do something when row is selected
}
public override int RowsInSection (UITableView tableview, int section)
{
return tableItems.Length;
}
public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
// request a recycled cell to save memory
UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier);
if (cell == null)
cell = new UITableViewCell (UITableViewCellStyle.Default, cellIdentifier);
cell.TextLabel.Text = tableItems[indexPath.Row];
return cell;
}
}
// This is the loop where i load items to my array and assign as a datasource to my tableview
string[] mycell = new string[200];
for (int i = 0; i < count; i++) {
var message = client.GetMessage (count, cancel.Token);
string m = Convert.ToString (message.Date);
DateTime mydate = Convert.ToDateTime (m);
string s = mydate.ToString ("MMMM dd, yyyy H:mm") + " " + "Subject:" + Convert.ToString (message.Subject) + " " + "Sender" + Convert.ToString (message.From) ;
mycell [i] = (Convert.ToString (s));
}
tblmytable = new UITableView (View.Bounds);
Add (tblmytable);
tblmytable.Source = new TableSource (mycell);
//---------EDIT----------------------
//Email Object
public class EmailItem
{
public DateTime RecievedDate{ get; set; }
public string Subject{ get; set; }
public string Sender{ get; set; }
public EmailItem(DateTime recieveddate, string subject, string sender)
{
RecievedDate=recieveddate;
Subject=subject;
Sender = sender;
}
}
//Function for isolate the today emails
public void TodayEmails(EmailItem[] emailarray, EmailItem[] myemailarray)
{
DateTime curdate = DateTime.Today;
for (int i = 0; i < emailarray.Length; i++) {
if (emailarray [i].RecievedDate == curdate) {
myemailarray [i] = emailarray [i];
}
}
}
答案 0 :(得分:1)
首先,我建议重写TodayEmails
方法:
public List<EmailItem> TodayEmails(List<EmailItem> emailArray)
{
foreach(var emailItem in emailArray)
{
if(emailItem.RecievedDate.Date == DateTime.Today.Date)
{
yield return emailItem;
}
}
}
然后重写表源的构造函数:
private List<EmailItem> _emailItems;
public TableSource (List<EmailItem> emailItems)
{
_emailItems= emailItems;
}
您应该覆盖NumberOfSections
以设置部分数量
public override int NumberOfSections(UITableView tableView)
{
return 3;
}
然后在方法RowsInSection
中,您应该为每个部分返回所需的行数,所以:
public override int RowsInSection(UITableView tableview, int section)
{
if(section == 0)
{
return TodayEmails(_emailItems).Count; //I don't know where your TodayEmails located, so added just the name of it
}
else if(section == 1)
{
return YesterdayEmails(_emailItems).Count; // The same as for TodayEmails
}
else
{
return OtherEmails(_emailItems).Count; // The same as for TodayEmails
}
}
最后在GetCell
方法中,您应该返回该部分所需的单元格:
public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier);
if (cell == null)
cell = new UITableViewCell (UITableViewCellStyle.Default, cellIdentifier);
if(indexPath.Section == 0)
{
cell.TextLabel.Text = TodayEmails(_emailItems)[indexPath.Row].Subject;
}
else if(indexPath.Section == 1)
{
cell.TextLabel.Text = YesterdayEmails(_emailItems)[indexPath.Row].Subject;
}
else
{
cell.TextLabel.Text = OtherEmails(_emailItems)[indexPath.Row].Subject;
}
return cell;
}
答案 1 :(得分:1)
我认为答案选择者可能已经回答了你的问题,我认为这是一个非常好的答案(并且可能值得接受的答案状态)。
我要补充的是,我不认为你想要从POP3服务器下载这些消息并将它们全部留在内存中来填充你的UITableView。
相反,我建议的是,你将消息下载到磁盘并保存一个数据库,用于显示在UITableView中显示的信息(主题,收件人,发件人,日期,也许是一个bool来说明是否或不是消息有附件,消息的位置保存在磁盘上,也许是消息体的前几行,如果你想做一些像iOS的默认邮件应用程序那样)然后使用数据库来填充你的UITableView
这样做的原因是电子邮件信息可能非常大,并且您不希望将其中的一堆邮件加载到内存中,而这些内存只是无法扩展到任何大量的邮件。
从sqlite数据库加载一个完整记录的屏幕也可能比加载和解析相同数量的消息更快(MimeKit非常快,但可能不比sqlite加载消息的简短摘要快)。
希望有所帮助。