朧の.Netの足跡
問合せ先:support@oborodukiyo.info サイト内検索はこちら
Swift UITableViewとUITableViewCellの関係





Swiftだと、UITableViewの中にあるセル、UITableViewCellにデータを入れて表示するというより、指定された行に表示するUITableViewCellを返して表示するという感じです。
UITableViewでは、複数の行を持つと思うので、表示したいデータは配列で持っておくと便利かと思います。
サンプルコードでは、表示したいデータを変数prefectureListに配列で持っています。


ViewController.swift

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    ViewData["atmark"]IBOutlet weak var tv: UITableView!
    ViewData["atmark"]IBOutlet weak var lblNote: UILabel!
    //UITableViewに表示するデータのリスト
    var prefectureList: [String] = []
    
    //UITableViewの行の数
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.prefectureList.count
    }

    //UITableViewに表示するUITableViewCellを返して表示する
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let i: Int = indexPath.row
        //メモリの使用量の圧縮のためリサイクル処理を入れています
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        
        if cell == nil {
            //リサイクルできない時は新規に作成
            cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
        }
        
        cell.textLabel?.text = self.prefectureList[i]
        
        return cell
    }
    
    //行をタップしたら、そのデータを次の画面に渡して遷移する
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let parameter: String = self.prefectureList[indexPath.row]
        
        self.tv.deselectRow(at: indexPath, animated: true)
        self.performSegue(withIdentifier: "showTwitterList", sender: parameter)
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        tv.delegate = self
        //ここで配列にデータを読み込んでいます
        if let path: String = Bundle.main.path(forResource: "saigai-prefList", ofType: "csv") {
            let enc = String.Encoding.utf8

            do {
                let s = try String(contentsOfFile: path, encoding: enc)
                let rawData = s.split(separator: "\r\n")
                
                for d in rawData {
                    
                    if String(d) == "都道府県" {
                        //何もしない
                    } else {
                        print(d)
                        self.prefectureList.append(String(d))
                    }
                }
                
            } catch {
                print("ファイルの内容の取得に失敗しました。")
            }
        }
        
        print("\(self.prefectureList.count)")
        
        self.tv.reloadData()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showTwitterList" {
            let destViewController = segue.destination as! TwitterListViewController
            //画面遷移先にデータを受け渡している
            destViewController.parameter = sender as! String
        }
    }
}
    








良いやや良い普通やや悪い悪い

投稿日時評価コメント