2017-11-07 9 views
1

テーブルビュー内の異なるセルに異なるイメージを追加したいのですが、これは私のコードであるカテゴリの構造体であるストリングのリストです。異なるセルの異なるイメージをプログラムでテーブルビューに追加する方法(スウィフト)

私は、リスト内の各項目のリスト(私は、その後のtableViewに追加されます)

import Foundation 
import UIKit 
import CoreLocation 
import Alamofire 

class NearbyPlaces { 
    static func getCategories() -> [QCategoryy] { 
     let list:[QCategoryy] = ["Art_gallery", "Amusement_park", "Zoo", "Bar", "Night_club", "Movie_theater", "Restaurant", "Shopping_mall", "Atm", "Gym", "Store", "Spa", "Museum", "Stadium", "Hardware_store", "Casino", "Library", "Painter", "Book_store", "Bowling_alley", "Cafe", "Clothing_store", ] 
     return list 
    } 

を作成するのはここ

struct QCategoryy { 
     var name:String 
     var image:UIImage 
     var isSelected = false 
     init(name:String, image.UIImage) { 
      self.name = name 
      self.image = image 
} 


    extension QCategoryy: ExpressibleByStringLiteral { 
     init(stringLiteral value: String) { 
      self.name = value 
     } 
     init(unicodeScalarLiteral value: String) { 
      self.init(name: value) 
     } 
     init(extendedGraphemeClusterLiteral value: String) { 
      self.init(name: value) 
     } 
    } 

とiは、セルサイズの特定の画像を追加したいですが、どのように僕にできる?

EDIT

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let identifier = "CATEGORY_CELL" 
     let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) 
     let selectedIndexPaths = tableView.indexPathsForSelectedRows 
     let rowIsSelected = selectedIndexPaths != nil && selectedIndexPaths!.contains(indexPath) 
     /* cell.accessoryType = rowIsSelected ? .checkmark : .none */ 
     cell.accessoryType = list[indexPath.row].isSelected ? .checkmark : .none 
     cell.textLabel?.text = list[indexPath.row].name 
     return cell 
    } 

答えて

0

カスタムセルを作成できる方が良いでしょう。 CellForRowAtIndexメソッドの行のすぐ下にある同じコードを使用してください。

static func getCategories() -> [QCategoryy] { 
     let list:[QCategoryy] = [QCategoryy(name: "name", image: UIImage(named: "imageName")!)] 
     return list 
    } 

延長コードなど

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
      let identifier = "CATEGORY_CELL" 
      let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) 
      let selectedIndexPaths = tableView.indexPathsForSelectedRows 
      let rowIsSelected = selectedIndexPaths != nil && selectedIndexPaths!.contains(indexPath) 

      cell.accessoryType = list[indexPath.row].isSelected ? .checkmark : .none 
      cell.textLabel?.text = list[indexPath.row].name 
      cell.imageView?.image = list[indexPath.row].image // make sure you are saving images in struct. 
      return cell 
     } 

initalize

init(name:String, image:UIImage) { 
      self.name = name 
      self.image = image 
     } 

として、変更FUNC:

extension QCategoryy: ExpressibleByStringLiteral { 
    init(stringLiteral value: String) { 
      self.name = value 
      self.image = UIImage() 
     } 
     init(unicodeScalarLiteral value: String) { 
      self.init(name: value, image: UIImage()) 
     } 
     init(extendedGraphemeClusterLiteral value: String) { 
      self.init(name: value, image: UIImage()) 
     } 
    } 
+0

リストの名前ごとに異なる画像が1つあります。 –

+0

@Tvendenはあなたの答えを自分のコードに更新しました。 :)。希望これは動作します –

+0

私はこれが動作すると思いますが、最後の質問、私は画像のリストを追加する必要がありますありがとう、ありがとう?初期化時に –

0

静的リストイスト、右?

なぜ画像URL(または必要なもの)をオブジェクトに追加しないのですか?それはあなたの問題を解決するでしょう^^。だから、行のためのセルでそれを呼び出すことができます:)

+0

はいリストが静的で、私は試すことができますが、私は知りませんどのように行うか –

+0

画像またはURL、文字列(何をあなたのQCカテゴリに)。 これをあなたのinitに追加してください。あなたの内線番号では分かりません。あなたはそれらを必要としますか?彼らは必要ですか? ない場合:ちょうどフォローにあなたの静的に変更します。 LETリストを= [QCategory(名称: "あなたの名前"、画像: "imagenameの")、...] にfuncのtableView(_のtableView:のUITableView、cellForRowAt indexPath: - : "your_cell_identifier") LETカテゴリ= NearbyPlaces.getCategory()[indexPath.row]
cell.image = category.image 戻りセルIndexPath)>のUITableViewCell { はセル= tableView.dequeueReusableCell(withIdentifierをしましょう!} – TMX

0
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
let cell = tableView.dequeueReusableCell(withIdentifier: "your_cell_identifier") 
let category = NearbyPlaces.getCategory()[indexPath.row] 
cell.image = category.image 
cell.name = category.name 
return cell! 

} 

を@TMXはあなたが使用することができ、言ったようにあなたがコメント^^

+0

私は今、自分のQCategory構造体が表示されたらコードを編集しましたが、拡張子にエラーがあります: "宣言はファイルスコープでのみ有効です" –

+0

私のanwerが更新されました。初期化時に追加する必要があります。 –

0

でそれを読んカント:

func cellForRow(at indexPath: IndexPath) -> UITableViewCell? 

参照:https://developer.apple.com/documentation/uikit/uitableview/1614983-cellforrow

そして:https://stackoverflow.com/a/8079765/7510062

そして、あなただけのコードに開始した場合このチュートリアルに従う必要があります:https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/CreateATableView.html

..それがどのように動作するかを理解するには、それはすばらしくなければなりません!

関連する問題