这是我的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的解决方案。
答案 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 let
或if let
s。