2014-09-23 16 views
7

में स्विफ्ट के साथ कस्टम टेबल व्यू सेल, इसलिए, मैंने बाईं ओर वाले लेबल के साथ एक कस्टम टेबल व्यू सेल बनाया और दाईं ओर एक UIImageView बनाया। लेबल 100 और UIImageView का एक टैग है 110xcode 6

मेरे कोड का एक टैग निम्नलिखित है:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("ThemeCell") as UITableViewCell 

    let theme = themes[indexPath.row] 

    var themeName = cell.viewWithTag(100) as? UILabel 
    themeName?.text = theme.themeName 

    let themeImage = cell.viewWithTag(110) as? UIImageView 
    themeImage?.image = UIImage(named: "test.png") 

    //1 
    //cell.imageView?.image = UIImage(named: "test.png") 

    println("The loaded image: \(themeImage?.image)") 

    return cell; 
} 

है के रूप में है, THEMENAME प्रदर्शित किया जाता है, लेकिन themeImage प्रकट नहीं होता है, हालांकि से println ऐसा लगता है कि छवि लोड हो गया है। यदि मैं 1 में कोड को अनदेखा करता हूं तो छवि कस्टम सेल में दिखाई देती है लेकिन निश्चित रूप से उचित स्थान पर दिखाई नहीं देती है क्योंकि यह आईआईएम में बनाए गए UIImageView में नहीं जोड़ा गया है। कोई विचार क्या मैं गलत कर सकता हूं? टैग सभी सही हैं। धन्यवाद

+0

मेरे पास आईबी में एक कस्टम सेल है ताकि UIImageView जोड़ने में सक्षम हो, लेकिन मैंने एक MyCustomCell क्लास नहीं बनाया है जो UITableViewCell को बढ़ाता है। मुझे नहीं लगता कि मुझे इसकी ज़रूरत है। मुझे बस UILabel और UIImageView की आवश्यकता है। –

+0

यह देखने के लिए निम्न कोड जोड़ें कि आपका 'UIImageView' प्रदर्शित किया जा सकता है:' थीम इमेज? .backgroundColor = UIColor.greenColor() '। –

+0

http://stackoverflow.com/questions/24170922/creating-custom-tableview-cells-in-swift/38470749#38470749 –

उत्तर

12

सबसे पहले, आपको ऑटो लेआउट तंत्र के साथ अपने विचार पिन करना होगा। ओपन इंटरफ़ेस बिल्डर, अपने कस्टम सेल के अंदर लेबल पर लेफ्ट क्लिक करें, तो उदाहरण के लिए निम्न करें:

  1. संपादक-> Pin-> चौड़ाई
  2. संपादक-> Pin-> ऊंचाई
  3. संपादक-> पिन -> अग्रणी अंतरिक्ष
  4. संपादक-> Pin-> शीर्ष अंतरिक्ष अपने कस्टम सेल के अंदर

Superview करने के लिए छवि के लिए एक ही Superview को

  1. संपादक-> Pin-> चौड़ाई
  2. संपादक-> Pin-> ऊंचाई
  3. संपादक-> Pin-> ट्रेलिंग अंतरिक्ष Superview को
  4. संपादक-> Pin-> शीर्ष अंतरिक्ष Superview को

फिर अपने सेल के लिए कस्टम क्लास बनाएं। उदाहरण

MyCustomCell.swift अपने सेल के लिए

import Foundation 
import UIKit 

class MyCustomCell: UITableViewCell { 
    @IBOutlet weak var myLabel: UILabel! 
    @IBOutlet weak var myImageView: UIImageView! 
} 

फिर सेट कस्टम वर्ग के लिए और तत्वों से कनेक्शन बना सकते हैं।

और अब tableViewController में आप मान अपने तत्वों के लिए टैग के बिना सेट कर सकते हैं:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    var cell: MyCustomCell = tableView.dequeueReusableCellWithIdentifier("ThemeCell") as MyCustomCell 

    let theme = themes[indexPath.row] 

    cell.myLabel.text = theme.themeName 
    cell.myImageView.image = UIImage(named: "test.png") 

    println("The loaded image: \(cell.myImageView.image)") 

    return cell; 
} 
3

ठीक है, तो गलती बिल्कुल uitable में लेकिन वास्तव में नहीं था autolayout सही तरीके से सेट नहीं किया गया था कि और छवि तालिकादृश्य के बाहर दिखाई दे रही है ...

संबंधित मुद्दे