如何在单个tableview单元中放置2个coredata属性。 (swift4)

时间:2018-07-04 19:01:13

标签: core-data attributes tableview swift4 cell

我的代码现在将一个核心数据属性称为“ lorde”,并将其放置在一个表视图单元格中。我希望单元格同时显示“ lorde”和其他属性“ num”。我希望两个属性都印在
cell.textLabel?.text

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let title = itemsName[indexPath.row]

    let cell = tazbleView.dequeueReusableCell(withIdentifier: "cell", for : indexPath)

    cell.textLabel?.text = title.value(forKey: "lorde") as? String


    return cell

}

2 个答案:

答案 0 :(得分:1)

我更喜欢这种方式 看起来更漂亮,而且只有一个属性存在就不会停止使用

let attr1 = title.value(forKey: "lorde") as? String
let attr2 = title.value(forKey: "num") as? String 
cell.textLabel?.text = [attr2, attr1].flatMap { $0 }.reduce("", +)

答案 1 :(得分:0)

您可以使用<!DOCTYPE html> <html> <head> <title>JavaScript Loan Calculator</title> <style> /* This is a CSS style sheet: it adds style to the program output */ .result {font-weight: bold; } /* For elements with class="result" */ #payment { text-decoration: underline; } /* For elemnt with id="payment" */ </style> </head> <body> <!-- This is an HTML form that allows the user to enter data and allows JavaScript to display the results it computes back to the user. The form elemnets are embedded in a table to improve their appearnce. The form itself is given the name "laondata", and the fields within the form are given names such as "interst" and "years". These field names are used in the JavaScript code that follows the form. Note that some of the form elements define "onchange" or "onclick" event handlers. These specify strings of JavaScript code to be executed when the user enters data or clicks on a button.--> <form name="loandata"> <table> <tr> <td>1) Amount of the loan (any currency):</td> <td><input type="text" name="principal" onchange="calculate();"></td> </tr> <tr> <td>2) Annual percentage rate of intrest:</td> <td><input type="text" name="interest" onchange="calculate();"></td> </tr> <tr> <td>3) Repayment period in years:</td> <td><input type="text" name="years" onchange="calculate();"></td> </tr> <tr><td></td> <td><input type="button" value="Compute" onclick="calculate();"></td> </tr> <tr><td><b>Payment Information:</b></td></tr> <tr> <td>4) Your monthly payment:</td> <td>$<span class="result" id="payment"></span></td> </tr> <tr> <td>5) Your total payment:</td> <td>$<span class="result" id="total"></span></td> </tr> <tr> <td>6) Your total intrest payments:</td> <td>$<span class="result" id="totalinterest"></span></td> </tr> </table> </form> <script language="JavaScript"> /* This is the JavaScript function that makes the example work. Note that this script defines the calculate() function called bt the event handlers in the form. The function reads cvalues from the form <input> fields using the names defined in the previous HTML code. It outputs its results into the named <span> elements. */ function calculate(){ //Get the user's input from teh form. Asume it is all valid. //Convert interest from a percentage to a decimal, and convert //from an annual rate to a monthly rate. Convert payment period in years //to the number of monthly payments var principal = document.loandata.principal.value; var interest = document.loandata.interest.value / 100 / 12; var payments = document.loandata.payments.value * 12; //Now compute the monthly payment figure, using esoteric math. var x = Math.pow(1 + interest, payments); var monthly = (principal*x*interest)/(x-1); //Get named <span> elements from the form. var payment = document.getElementByID("payment"); var total = document.getElementByID("total"); var totalinterest = document.getElementByID("totalinterest"); //check that the result is a finite number. If so, display the //results by setting the HTML content of each <span> element. if (isFinite(monthly)) { payment.innerHTML = monthly.toFixed(2); total.innerHTML = (monthly * payments).toFixed(2); totalinterest.innerHTML = ((monthly*payments)-principal).toFixed(2); } //Otherwise the user's input was probably invaild, so display nothing else{ payment.innerHTML = ""; total.innerHTML = ""; totalinterest.innerHTML = ""; } } </script> </body> <html> 运算符将两个字符串连接起来。

+