2014-09-22 78 views
2

我試圖添加許多細節(字段)以在TableView中顯示。通過CONCAT各個領域,並把在detailTextLabel如:如何在TableView中添加2個以上的detailTextLabel(Swift)

來源:

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{ 

    let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier:"test") 

    cell.textLabel.text = taskMgr.tasks[indexPath.row].name 
    cell.detailTextLabel.text = taskMgr.tasks[indexPath.row].desc 

    return cell 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ 
    let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Test") 
    cell.textLabel?.text=tasksList[indexPath.row].name 
    var yString = tasksList[indexPath.row].desc 
    var xString = tasksList[indexPath.row].amnt 

    var zString = xString+" "+yString 
    cell.detailTextLabel?.text=zString 
    return cell 
} 

誰能指出任何其他方式獲得更好的代碼,許多領域在視圖中顯示。

+2

您可以使用自定義單元格。 – 2014-09-22 05:27:31

+1

我搜索了「自定義單元格」找到了相關的代碼和導師。 Thks – 2014-09-22 06:34:19

+0

歡迎@ user3528013 – 2014-09-22 06:55:26

回答

0

我認爲你的第一種方法很好。在原型單元格中的故事板中,您可以放置​​多個UILabels。然後像你一樣更新每個標籤。您可以將標籤放置在xib中或使用代碼創建它們。但是,擁有多個標籤然後單獨更新它們沒有任何問題。這可以提供更大的連接靈活性,因爲您可以根據需要使每個標籤中的文本合理化,從而使tableview更加美觀。所以每個單元格行都與上面的單元行保持一致。

+0

我來自PHP和當然新的Swift/Xcode。當從數據庫到數組重新輸入數據時,做這樣的每一個都很困難。我無法賦予數組
[cell.textLabel?.text = tasksList [indexPath.row] .name var yString = tasksList [indexPath.row] .desc var xString = tasksList [indexPath.row] .amnt] 錯誤'任何對象'都沒有成員名稱desc – 2014-09-22 09:09:50

相關問題