使用NSMutableDictionary将UITableView数据发送到HTML电子邮件正文

时间:2013-11-21 16:52:55

标签: html ios objective-c uitableview

我正在尝试找出将UITableView数据的内容显示到HTML电子邮件正文中的最佳方式。

每个表格单元格都有一个日期和说明,它们位于自定义UITableViewCell类中。

问题在于我使用大量基于核心数据的日期计算来显示UITableView中的结果,这意味着我必须复制所有这些以创建HTML数据的数据因为我不是简单地将核心数据转储到表中,而是显示计算的日期。

据我所知,我的选择是:

  1. 当我对cellForRowAtIndexPath方法中的每个日期进行计算时,我可以将结果添加到NSMutableDictionary,然后循环创建HTML数据。

    < / LI>
  2. 找出循环遍历表格中单元格数据的方法。我想尝试走这条路线,因为在某些时候可能需要允许用户手动“覆盖”计算的日期,所以我希望这是导出为HTML的日期。

    < / LI>

    这是电子邮件代码,如果它有助于理解我正在尝试做什么。我正在试图从阵列中拉出物品,但没有成功。

    // name of array is results
    
    
    if ([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
        [mailer setSubject:subject];
    
        NSString *emailTop = [NSString stringWithFormat:@"<html><body><table border=1><tr><th>Date</th><th>Event</th></tr>"];
    
    
        for(int i=0;i<[self.results count];i++){
    
            NSString *expDate = [self.results objectAtIndex:i];
            NSString *expDesc = [self.results objectAtIndex:i]];
    
            emailBody = [NSString stringWithFormat:@"<tr><td>%@</td><td>%@</td><tr>",expDate,expDesc];
    
        }
    
        NSString *emailBottom = [NSString stringWithFormat:@"</table></body></html>"];
        NSString *emailComplete = [NSString stringWithFormat:@"%@%@%@",emailTop,emailBody,emailBottom];
    
    
        [mailer setMessageBody:emailComplete isHTML:YES];
        [self presentViewController:mailer animated:YES completion:nil];
    
    }
    

    根据要求...注意结果数组是原始核心数据拉,不要与我试图为每个单元格数据创建的数组混淆

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *CellIdentifier = @"CellResults";
    
    // Configure the cell...
    
    ResultsCell *resultscell = (ResultsCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    NSManagedObject *result = [self.results objectAtIndex:indexPath.row];
    
    
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MMMM dd, yyyy"];
    [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
    
    //  populate the cells
    [resultscell.labelEventDesc setText:[NSString stringWithFormat:@"%@",[result valueForKey:@"eventdesc"]]];
    [resultscell.labelWeeksOut setText:[NSString stringWithFormat:@"-%@ weeks",[result valueForKey:@"weeksout"]]];
    
    
    //  calculate the event dates
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
    
    NSString *weeks = [NSString stringWithFormat:@"-%@",[result valueForKey:@"weeksout"]];
    int intWeeks = [weeks intValue];
    
    [offsetComponents setWeek:intWeeks];
    
    NSString *wkday = [NSString stringWithFormat:@"%@",[result valueForKey:@"dow"]];
    int targetWeekday = [wkday intValue];
    
    
    NSDate *eventDt= [gregorian dateByAddingComponents:offsetComponents toDate:[self trialDate] options:0];
    NSDateFormatter* dateF = [[NSDateFormatter alloc] init];
    [dateF setDateFormat:@"EEEE, MMMM dd, yyyy"];
    [dateF setTimeZone:[NSTimeZone localTimeZone]];
    
    if (targetWeekday==0) {
    
        //  if there is no preference for a specific day of week
        NSString *eventDtforDisplay = [dateF stringFromDate:eventDt];
        resultscell.labelEventDesc.numberOfLines=0;
        [resultscell.labelEventDesc sizeToFit];
        [resultscell.textEventDate setText:[NSString stringWithFormat:@"%@",eventDtforDisplay]];
    
    
    } else {
    
        //if there is a day of week preference
        NSDateComponents *fComp = [gregorian components:NSCalendarUnitWeekday fromDate:eventDt];
        NSInteger weekday = fComp.weekday;
    
        NSDateComponents *c = [[NSDateComponents alloc]init];
    
        c.day = targetWeekday - weekday;
    
        NSDate *resultDate = [gregorian dateByAddingComponents:c toDate:eventDt options:0];
        NSDateFormatter* newDateF = [[NSDateFormatter alloc] init];
        [newDateF setDateFormat:@"EEEE, MMMM dd, yyyy"];
        [newDateF setTimeZone:[NSTimeZone localTimeZone]];
        NSString *eventDtforDisplay = [newDateF stringFromDate:resultDate];
        resultscell.labelEventDesc.numberOfLines=0;
        [resultscell.labelEventDesc sizeToFit];
        [resultscell.textEventDate setText:[NSString stringWithFormat:@"%@",eventDtforDisplay]];
    }
    
        return resultscell;
    

    }

0 个答案:

没有答案