I'm using Xcode 7.1 and Swift 2. I have an array
var ingredientsList: [String] = []
that will contain data that is displayed in a UITableView
. I want to separate each String
in the array
and assign to a new string
variable to later add to some html
. I looked here and it was very confusing and also dealt with int
. I tried to apply it with mine, but obviously using string
instead but it didn't work. Can someone please help me? An example of the array data is: cheese, pepperoni, sauce, olives, onions, sausage
. Thank you.
My ViewController
with a UITableView
is:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "IngredientTableViewCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! IngredientTableViewCell
cell.ingredientLabel.text = ingredientsList[indexPath.row]
// TOTAL of 50 ingredients Max
if indexPath.row == 0 {
if ingredientsList[0] != "" {
ingredientItem0 = ingredientsList[0]
}
}
if indexPath.row == 1 {
if ingredientsList[1] != "" {
ingredientItem1 = ingredientsList[1]
}
}
if indexPath.row == 2 {
if ingredientsList[2] != "" {
ingredientItem2 = ingredientsList[2]
}
}
if indexPath.row == 3 {
if ingredientsList[3] != "" {
ingredientItem3 = ingredientsList[3]
}
}
if indexPath.row == 4 {
if ingredientsList[4] != "" {
ingredientItem4 = ingredientsList[4]
}
}
if indexPath.row == 5 {
if ingredientsList[5] != "" {
ingredientItem5 = ingredientsList[5]
}
}
// ... it goes all the way down to a total of 50 ingredients
return cell
}
I have a UIBarButtonItem
that performs an action of exporting the [String]
data as a PDF and allows the user to email it out.
My export code is:
let html =
/*HEADING*/"<head><center><br><h1><b>** <u>\(nameTextField.text!)</b></u> **</h1></br></center></head>" +
/*DIVIDER*/"<center>- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -</center>" +
/*BODY*/"<p><font size=5><br>\(ingredientItem0)</br> <br>\(ingredientItem1)</br> <br>\(ingredientItem2)</br> <br>\(ingredientItem3)</br> <br>\(ingredientItem4)</br> <br>\(ingredientItem5)</br> ... <br>\(ingredientItem49)</br></font></p>"
let fmt = UIMarkupTextPrintFormatter(markupText: html)
// Assign print formatter to UIPrintPageRenderer
let render = UIPrintPageRenderer()
render.addPrintFormatter(fmt, startingAtPageAtIndex: 0)
// Assign paperRect and printableRect
let page = CGRect(x: 0, y: 0, width: 595.2, height: 841.8) // A4, 72 dpi
let printable = CGRectInset(page, 0, 0)
render.setValue(NSValue(CGRect: page), forKey: "paperRect")
render.setValue(NSValue(CGRect: printable), forKey: "printableRect")
// Create PDF context and draw
let pdfData = NSMutableData()
UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil)
for i in 1...render.numberOfPages() {
UIGraphicsBeginPDFPage();
let bounds = UIGraphicsGetPDFContextBounds()
render.drawPageAtIndex(i - 1, inRect: bounds)
}
UIGraphicsEndPDFContext();
Right now, in my UITableView
code above, I have it assign each indexPath.row
to a String
variable
I've created. I have it set for up to 50 ingredients. I don't like that I've set a limit and if the user only has 4 ingredients and or more than 50. In my pdf export, it acts like there are 50 and prints blank lines for them. So a potentially short recipe will have blank pages from the blank ingredients that I assigned that weren't there. Make sense? It's ugly, but I don't know how else to do it.
For loop through your ingredients list in your export code and append your html that wraps your ingredient list item to the html
var html =
/*HEADING*/"<head><center><br><h1><b>** <u>\(nameTextField.text!)</b></u> **</h1></br></center></head>" +
/*DIVIDER*/"<center>- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -</center>" +
/*BODY*/"<p><font size=5>"
for var i = 0; i < ingredientsList.length; i++ {
html += "<br>\(ingredientsList[i] as String)</br> "
}
Something like that....