BeginningDeveloperFeaturediOS DeveloperNewbieProgramming LanguageSwift

เขียนแอพ iPhone ภาษา Swift กับการแทรก Image บน UITableView

เขียนแอพพลิเคชัน iPhone กับวิธีการเพิ่มรูปภาพ Images ของ UITableViewCell บน UITableView และเรียกผ่านเว็บไซต์ ด้วยภาษา Swift บน Xcode6 BETA สำหรับผู้เริ่มต้น

ก่อนอื่นลองศึกษาบทความก่อนหน้านี่ก่อนครับไม่น่ายากอะไร

และตัวอย่างที่เราจะหยิบมาพัฒนาต่อก็คือ ตัวอย่างจากบทความนี้

หารูปภาพที่เราต้องการจะนำมาทำเป็นภาพ Thumbnail ของ Cell ก่อน ในตัวอย่างผมเลือกภาพนี้ (ข้างล่าง) นำภาพนี้ไปวางใน Project ของเราครับ ตั้งชื่อว่า icon.png

Screen Shot 2557-06-12 at 7.01.14 AM

ลอง Preview ดูหน่อย

Screen Shot 2557-06-12 at 7.01.41 AM

แก้ไขฟังก์ชัน นี้ให้ทำการดึงไฟล์รูปมาแสดงครับ

//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")
        
        return tableCell
    }

ทดสอบ Run ตัวแอพพลิเคชันของเราครับ

Screen Shot 2557-06-12 at 7.03.40 AM

ต่อมาคือการแสดงผล รูปภาพ ไปแสดงใน Cell เหมือนกันแต่เป็นการโหลดผ่าน URL ผมเลือก ภาพของ Facebook มันง่ายดี รูปภาพที่ต้องการก็คือภาพ Profile ผมเอง http://graph.facebook.com/banyapon/picture ครับ

เราก็แค่ เขียน Code เพิ่มเข้าไปดังนี้ครับ ก็เป็นอันเสร็จเรียบร้อย

//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)"

        var imgWebURL: NSURL = NSURL(string:"http://graph.facebook.com/banyapon/picture/")
        var imgData: NSData = NSData(contentsOfURL: imgWebURL)
        tableCell.image = UIImage(data: imgData)
        
        return tableCell
    }

ทดลอง Run ตัวแอพพลิเคชันของเราดูครับ ไม่มี Source Code เช่นเคยเพราะคิดว่าไม่น่าจะยากอะไรครับ

Screen Shot 2557-06-12 at 7.05.34 AM

บทความที่เกี่ยวข้อง

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 ของเรานะครับ, ถือว่าช่วยเหลือกัน