【问题标题】:Obtain a data from a variable inside a struct in a different class Swift从不同类 Swift 的结构内的变量中获取数据
【发布时间】:2018-12-17 07:48:37
【问题描述】:

我正在尝试从这个结构中的变量中为我的 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 文本工作正常

【问题讨论】:

  • 您应该使用switch。我想有一个不同的问题类型,switch 会告诉你。另外,没有理由使用静态方法,使用普通方法和self.type

标签: ios swift struct


【解决方案1】:

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

使用item的属性

labelTester.text = item.tester

【讨论】:

  • facepalm 谢谢!每天学习新东西,但我想知道“项目”是如何工作的?我的意思是我只将它声明为 issueType
  • @Nodoodle arrayIssue 对象的数组,item 是该数组中的某个Issue 对象,其索引等于indexPath.row。这个item 具有typetester 等属性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-05
  • 1970-01-01
  • 2018-06-21
相关资源
最近更新 更多