对成员'下标'的模糊引用

时间:2016-10-11 14:56:21

标签: ios swift uitableview swift3

这是我的Swift 2代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell: UITableViewCell!
    cell = tableView.dequeueReusableCellwithIdentifier: forWithIdentifier("idCellChannel", forIndexPath: indexPath as IndexPath)

    let channelTitleLabel = cell.viewWithTag(10) as! UILabel
    let thumbnailImageView = cell.viewWithTag(12) as! UIImageView

    let channelDetails = channelsDataArray[indexPath.row]
    channelTitleLabel.text = channelDetails["title"] as? String

    // Error  Ambiguous reference to member 'subscript'
    thumbnailImageView.image = UIImage(data: NSData(contentsOfURL: NSURL(string: (channelDetails["thumbnail"] as? String)!)!)!)

    return cell
}

请给我一个Swift 3的解决方案。

1 个答案:

答案 0 :(得分:0)

您需要告诉编译器channelsDataArray[indexPath.row]Dictionary,以便您可以subscript。请尝试以下代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let channelsDataArray = [[String: AnyObject]]()//This is your array of dictionaries
    let cell = tableView.dequeueReusableCell(withIdentifier: "idCellChannel", for: indexPath) as! UITableViewCell

    let channelTitleLabel = cell.viewWithTag(10) as! UILabel
    let thumbnailImageView = cell.viewWithTag(12) as! UIImageView
    let channelDetails = channelsDataArray[indexPath.row]
    channelTitleLabel.text = channelDetails["title"] as? String
    thumbnailImageView.image = UIImage(data: NSData(contentsOf: NSURL(string: (channelDetails["thumbnail"] as? String)!)! as URL)! as Data)

    return cell
}

并检查nil,因为您要在应用程序崩溃的大部分地方强制解包。使用guard letif let s。