这是我的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的解决方案。
发布于 2016-10-11 23:09:01
你需要告诉编译器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。
https://stackoverflow.com/questions/39980243
复制相似问题