DeveloperFeaturediOS DeveloperProgramming LanguageSwift

เขียนแอพ iPhone ภาษา Swift ใช้ ข้อมูล Array แสดงผลบน UITableView

ตัวอย่างวิธีการเขียนแอพพลิเคชันบน iPhone ระบบปฏิบัติการ iOS8 ด้วยภาษา Swift ในการนำชุดตัวแปรข้อมูล Array มาแสดงผลผ่าน UITableView อย่างง่ายด้วย XCode6 BETA 

ตัวอย่างเดียวหากินนาน เลยกับภาษา Swift ซึ่งในรอบนี้ก็ยังคงขอแนะนำให้ศึกษาบทความก่อนหน้านี่ก่อนเหมือนเดิมเพื่อความเข้าใจ

รอบนี้จะใช้ ตัวอย่าง จากบทความ

มาใช้ประกอบการพัฒนาแอพพลิเคชัน เริ่มต้นให้ลอง เปิดไฟล์ ViewController.swift ขึ้นมา

พิจารณาส่วนบรรทัดนี้ครับ

import UIKit

class ViewController: UIViewController, UITableViewDelegate{

    override func viewDidLoad() {

ให้เพิ่ม UITableViewDataSource และ IBOutlet ขึ้นมาใหม่ แก้ไขจากเดิมให้เป็นดังนี้

import UIKit

class ViewController: UIViewController, UITableViewDelegate,UITableViewDataSource{

@IBOutlet
    var tableData: UITableView
    var listData: String[]!
    
    
    override func viewDidLoad() {

โดยมีการกำหนด ตัวแปร tableData ขึ้นมาเก็บค่าของ UITableView และสร้างชุดข้อมูล String ขึ้นมา ต่อมาให้แก้ไขที่ Function viewDidLoad() ครับ

 override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        listData=[
            "Game of Thrones 1",
            "Game of Thrones 2",
            "Game of Thrones 3",
            "Game of Thrones 4",
            "True Detective 1"]
        self.tableData.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        
    }

สร้างชุด Array ข้อมูลของรายการหนังซีรีย์ที่ดูอยู่ก่อนแล้วกัน เก็บลงในตัวแปร Array ชื่อ listData

ต่อมาเราจับข้อมูลมาโชว์ลงใน UITableView ทันทีครับ โดยการแก้ไข สอง Function นี้

//Table
    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{
        return 10
        
    }
    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell{
        let tableCell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
        
        tableCell.text = "Title of Row: #\(indexPath.row)"
        tableCell.detailTextLabel.text = "Detail of Row: #\(indexPath.row)"
        //tableCell.image = UIImage(named: "icon")
        
        
        var imgWebURL: NSURL = NSURL(string:"http://graph.facebook.com/banyapon/picture/")
        var imgData: NSData = NSData(contentsOfURL: imgWebURL)
        tableCell.image = UIImage(data: imgData)
        
        return tableCell
    }

ให้เป็น

//Table
    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{
        return self.listData.count
    }
    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell{
        let tableCell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
        
        tableCell.text=self.listData[indexPath.row]
        
        var imgWebURL: NSURL = NSURL(string:"http://graph.facebook.com/banyapon/picture/")
        var imgData: NSData = NSData(contentsOfURL: imgWebURL)
        tableCell.image = UIImage(data: imgData)
        
        return tableCell
    }

อธิบายคือการ Return ค่าแถวให้เป็น return listData และ ให้ แสดงผลข้อมูล Cell

 tableCell.text=self.listData[indexPath.row]

ไปที่ MainStoryBoard ทำการกด Ctrl แล้วคลิกจาก ViewController iCon ไปที่ TableView เหมือนรูป

Screen Shot 2557-06-12 at 6.21.20 PM

ทำการ Run แอพพลิเคชัน ของเราอีกครั้ง ก็เรียบร้อยครับ

Screen Shot 2557-06-12 at 6.23.51 PM

 

Asst. Prof. Banyapon Poolsawas

อาจารย์ประจำสาขาวิชาการออกแบบเชิงโต้ตอบ และการพัฒนาเกม วิทยาลัยครีเอทีฟดีไซน์ & เอ็นเตอร์เทนเมนต์เทคโนโลยี มหาวิทยาลัยธุรกิจบัณฑิตย์ ผู้ก่อตั้ง บริษัท Daydev Co., Ltd, (เดย์เดฟ จำกัด)

Related Articles

Back to top button
Game & Mobile Development AR VR XR
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Adblock Detected

เราตรวจพบว่าคุณใช้ Adblock บนบราวเซอร์ของคุณ,กรุณาปิดระบบ Adblock ก่อนเข้าอ่าน Content ของเรานะครับ, ถือว่าช่วยเหลือกัน