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

标签 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

}

附注labelStatus 文本工作正常

最佳答案

设置 labelStatus 的文本是有效的,因为您正在调用 Issue 类型的静态函数,并且该函数返回 String。在第二种情况下,您尝试获取结构类型的属性,而不是特定item的属性。

使用item的属性

labelTester.text = item.tester

关于ios - 从不同类 Swift 中的结构内的变量获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53810868/

相关文章:

swift - 无法更改 UILabel [Swift] 上的 UIFont 大小

ios - 在物理设备上测试我的应用程序(使用 swift)时崩溃,线程 1 : EXC_BREAKPOINT (code=1, 子代码 = 0x120021088)

c - 动态分配的结构数组

c++ - 如何检查元素是否在由结构组成的 vector 中?

c# - 将 C++ 结构转换为 C#

objective-c - CGContextShowTextAtPoint 和西里尔文字

ios - 从 iOS 应用程序调用网络服务

ios - SplitViewController 应用程序在加载 webview 时崩溃

ios - 在 Xcode 中, Storyboard上的所有对象现在都是不可见的,但在更新帧后仍然可以看到帧和约束

ios - 从 firebase 获取的数据有时不会添加到单元格中,尽管之前已经获取过