【问题标题】:My codes do not work for sections in TableView我的代码不适用于 TableView 中的部分
【发布时间】:2016-08-13 09:11:49
【问题描述】:

我需要在 TableView 中设置部分。每个部分都有不同的值类型,请参阅我的代码。但它不起作用。请建议在部分中设置值的正确方法。请参阅屏幕截图以获取错误消息。注意:我删除了 var detailsInSection 末尾的“)”,因为它无法正确显示。

error message

var sectionTitles = ["WebSite", "Date Saved", "Document Used","Add Notes"]

var detailsInSection = [([String], [NSDate],[AnyObject],[String])]()

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return detailsInSection[section].count
}

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

    let cell = tableView.dequeueReusableCellWithIdentifier("DetailsCell")

    cell?.textLabel!.text = detailsInSection[indexPath.section][indexPath.row]

    return cell!
}

【问题讨论】:

  • 您好,返回 true,谢谢,请在 var detailsInSection 末尾添加一半“)”。
  • 迈克,请使用edit 按钮自己完成。 :) 谢谢。
  • 完成。我以为我没有权限。
  • 您总是在自己的帖子上。在其他情况下,它确实与您的声誉得分有关。

标签: ios swift uitableview


【解决方案1】:
var detailsInSection = [([String], [NSDate],[AnyObject],[String])]

上面是一个元组数组。我从您的其余代码中猜测您想要一个数组数组。

var detailsInSection = [[String](), [NSDate](), [AnyObject](), [String]()]

您的数组将有 4 个元素,每个元素都是一个数组。

如果您想处理不同的类型,请使用 switch 语句。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("DetailsCell")!
    switch indexPath.section {
        case 0:
cell.textLabel!.text = detailsInSection[indexPath.section][indexPath.row]
        case 1:
             let date = detailsInSection[indexPath.section][indexPath.row]
             // Do some date formatting etc
             cell.textLabel!.text = PUT YOUR FORMATTED DATE HERE
        case 2:
             // Do any type casting etc.
             let object = detailsInSection[indexPath.section][indexPath.row] 
             // Convert object to whatever is needed in text format.
             cell.textLabel!.text = PUT YOUR OUTPUT HERE
        case 3:
             // This is a string so just assign it.
             cell.textLabel!.text = detailsInSection[indexPath.section][indexPath.row]
        default:
             // What to do here? Should never happen.
    }
    return cell
}

【讨论】:

    【解决方案2】:

    不是存储由 (String, NSDate, AnyObject, String) 组成的元组,而是为每个部分存储一个数组。这将为您提供更大的灵活性和更轻松的访问。将您的代码更改为:

    var detailsInSection = [[AnyObject]]() // Note the type is 'AnyObject', so you can still store your strings, dates etc. here
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return detailsInSection[indexPath.section].count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("DetailsCell")!
        cell.textLabel!.text = detailsInSection[indexPath.section][indexPath.row].description // Description will transfer your object to a string. If you want to change this behavior, you must process your objects according to their type.
        return cell
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-10
      • 2021-08-26
      • 2016-01-01
      • 1970-01-01
      • 2013-11-05
      • 1970-01-01
      • 2014-05-25
      相关资源
      最近更新 更多