2016-10-11 104 views
0

這裏是我的斯威夫特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 
} 

請給我一個解決方案,斯威夫特3.

+0

嘗試用'let channelDetails = channelsDataArray [indexPath.row] as! Dictionary

+0

請用'channelsDataArray'聲明更新您的問題。 – rmaddy

+0

還要說明哪一行實際上是在給出錯誤。您的錯誤評論是否適用於該行之前或之後的行? – rmaddy

回答

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。

+0

改變'channelsDataArray'的聲明來指定數組中的內容不是更好嗎?那麼你不需要在訪問它的每行代碼上都進行強制轉換。 – rmaddy

+0

是的,這是真的,更新我的代碼與Swift3 – Santosh