有什么方法可以复制 UITableView 的内容吗?这要记住它与表格在同一视图中的事实,但是,单元格是自定义的(但是只有 4 行文本,每行包含 4 个标签)。理想情况下,我希望通过电子邮件发送类似 excel 的表格,但我想找到一种方法来复制每个单元格(并保持它们之间的格式)并以有用的方式发送。(信息是反馈,有几个类别被评为 5 分!)
问问题
52 次
2 回答
0
没有用于执行此操作的 API,您必须自己生成包含表格内容的文件。就一张一张的往桌子上走。
于 2013-09-25T15:20:18.823 回答
0
You can save it as a CSV using this code (am looking now)
+ (NSString *)returnCreatedCSVFileNameFromColumns:(NSArray *)columns andDetails:(NSArray *)details {
//Create CSV string:
__block NSString *csvString = [[NSString alloc] init];
NSMutableArray *detailsArray = [[NSMutableArray alloc] init];
NSMutableArray *columnsArray = [[NSMutableArray alloc] init];
//Current Column:
for (TableColumn *column in columns) {
//Add the title for the column we are looping through:
csvString = [csvString stringByAppendingString:[NSString stringWithFormat:@"%@,", column.displayName]];
}
[columnsArray addObject:csvString];
csvString = @"";
for(Individual *individual in details)
{
for (TableColumn *tableColumn in columns)
{
csvString = [csvString stringByAppendingString:[NSString stringWithFormat:@"%@", [[tableColumn columnValueForIndividual:individual] stringByReplacingOccurrencesOfString:@"," withString:@"/"] ? : @""]];
}
csvString = [csvString stringByAppendingString:@","];
}
csvString = [csvString stringByAppendingString:@"\n"];
}
[detailsArray addObject:csvString];
csvString = @"";
csvString = [csvString stringByAppendingString:[NSString stringWithFormat:@"%@\n%@", [columnsArray objectAtIndex:0], [detailsArray objectAtIndex:0]]];
//Save csv to docs folder:
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [path objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"current_share.csv"];
NSData *csvFileData = [csvString dataUsingEncoding:NSASCIIStringEncoding];
if ([csvFileData writeToFile:filePath atomically:YES]) {
DLog(@"File written to: %@", filePath);
}
else
DLog(@"Saving file error");
return filePath;
}
I've used this to create a CSV file and return the local file path which can then be emailed somewhere. Obviously your data structure may be different but you should be able to get the general idea ... ? If not please let me know (I have omitted some code).
于 2013-09-25T15:28:38.693 回答