从不同类Swift中的结构内的变量获取数据

时间:2018-12-17 07:48:38

标签: ios swift struct

我正在尝试从该结构中的UITableView单元格中的变量获取数据

struct Issue {
 var id: String
 var tester: String
 var type: issueType
 var title: String
 var appName: String
 var desc: String
 var date: Date

 static func type(_ item: issueType) -> String {
     if item == .major {
         return "major"
     }
     else if item == .blocker {
         return "blocker"
     }
     else if item == .minor {
         return "minor"
     }
     return ""
 }
}

但是每次我尝试获取标签的数据时,它都不会显示。这是我的viewController中的代码

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)

    let item = array[indexPath.row]

    let topLine = cell.viewWithTag(1)
    let labelStatus = cell.viewWithTag(2) as! UILabel
    let labelTester = cell.viewWithTag(3) as! UILabel
    let labelTitle = cell.viewWithTag(4) as! UILabel
    let labelAppName = cell.viewWithTag(5) as! UILabel
    let labelDesc = cell.viewWithTag(6) as! UILabel
    let labelDate = cell.viewWithTag(7) as! UILabel
    let labelId = cell.viewWithTag(8) as! UILabel

    labelStatus.text = Issue.type(item.type).capitalized
    labelTester.text = Issue.tester

    return cell

}

P.S。 labelStatus文本可以正常工作

1 个答案:

答案 0 :(得分:0)

设置labelStatus的文本是可行的,因为您要在类型Issue上调用静态函数,并且此函数返回String。在第二种情况下,您尝试获取struct类型的属性,而不是某些item的属性。

使用item的属性

labelTester.text = item.tester