ios - 我的代码不适用于 TableView 中的部分

标签 ios swift uitableview

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

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!
}

最佳答案

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
}

关于ios - 我的代码不适用于 TableView 中的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36748059/

相关文章:

ios - 使用自动布局将自定义 UIView 垂直和水平居中

ios - 无法在 iOS 9 beta/9.3 上更改 WKWebView 的滚动率

swift - 无法将类型 'AuthDataResult?' 的值转换为预期参数类型 'User'

swift - 在 Swift 中刷新后刷新控件不会向上滑动屏幕

ios - Swift协议(protocol)问题,如何表达

swift - 快速向上滚动时,UITableViewCells 在标题后面可见

iphone - UITableView自定义单元格滚动后内容消失

iphone - UIPickerView 旋转效果

iphone - UITableview 和数据源为 NSMutableArray

iphone - Objective-C:每次上下滚动时,UITableView 单元格都会被覆盖