26

たくさんの質問を読みましたが、答えが見つからないようです

TableView がありますが、セルに画像を追加できません

cell.imageView.image = UIImage(named: "osx_design_view_messages")

基本的に回答から質問にコピーしたため、このコードは機能するはずですが、何らかの理由で機能しません。助言がありますか?

ちなみにswift使ってる

 override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
    let cellIdentifier = "cell"

    var cell : UITableViewCell

    cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as UITableViewCell
    cell.textLabel.text = self.recipies[indexPath.row]
    var image : UIImage = UIImage(named: "osx_design_view_messages")
    return cell
}
4

2 に答える 2

41

問題の考えられる原因は次のとおりです。

  1. セル プロトタイプには UIImageView を含める必要があります。ストーリーボードを使用している場合は、セル プロトタイプを選択し、属性インスペクター (右側のパネル) でそのスタイルが「基本」に設定されていることを確認します。

  2. セル ID が、ストーリーボードのプロトタイプ セルに入力された ID と一致しません。属性インスペクタでも「識別子」に「セル」が入力されていることを確認します。

  3. 画像がファイルから正しく読み込まれていません。これをテストするには、イメージを初期化した後に次の行を追加します。

    println("The loaded image: \(image)")

ストーリーボードにすべてが設定されていることを確認したら、次のコードを使用して実行してみてください。

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
    {
        let cellIdentifier = "cell"

        var cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as UITableViewCell

        cell.textLabel.text = self.recipes[indexPath.row]

        var image : UIImage = UIImage(named: "osx_design_view_messages")
        println("The loaded image: \(image)")
        cell.imageView.image = image

        return cell
    }
于 2014-06-07T07:22:00.483 に答える
2

スイフト3

// 画像がサーバーにある場合はこれを使用します。

// URL から画像を取得しています。

// Xcode から画像を設定できます。

// imageView にタグを割り当てる

  1. 画像の URL は配列名 = サムネイル、つまり self.thumbnail[indexPath.row] にあります。
  2. on UITableviewCell セルに imageView を配置します
  3. UIimageView を選択し、ストーリーボードからタグを割り当てます。

    let pictureURL = URL(string: self.thumbnail[indexPath.row])!
    let pictureData = NSData(contentsOf: pictureURL as URL)
    let catPicture = UIImage(data: pictureData as! Data)
    var imageV = UIImageView()
    imageV = cell?.viewWithTag(1) as! UIImageView
    imageV.image = catPicture
    
于 2017-03-27T12:10:46.323 に答える