2013-04-09 4 views
15

मैं एक यूटिव्यूव्यूसेल सबक्लास के लिए ऑटोलायआउट का उपयोग करने की कोशिश कर रहा हूं जिसे मैं बना रहा हूं और लेआउट बाधा निर्माण कोड को रखने के तरीके पर कुछ सलाह की सराहना करता हूं। मैं चारों ओर खोज कर रहा हूं और दृश्य जानकारी में सबव्यूव्यू जोड़ने के बाद बाधाओं को जोड़ने के बारे में मुझे जो सारी जानकारी मिलती है। जहां तक ​​मुझे पता है कि ViewDidLoad एक uitableviewcell उपclass के लिए एक विकल्प नहीं है।जहां subclassed uitableviewcell के लिए ऑटोलायआउट बाधाएं बनाना है?

मैं कस्टम सेल बनाने और गतिशील रूप से इसे मेरे कोड में आवंटित करने के लिए इंटरफ़ेस बिल्डर का उपयोग कर रहा हूं। वहां कुछ भी खास नहीं है ... मैं uitableviewcell subclass ताकि मैं सेल में एक कस्टम uiview जोड़ सकते हैं। दोबारा, विशेष रूप से पृथ्वी टूटने से कुछ भी नहीं ... मेरी कठिनाई तब आती है जब मैं अपने कस्टम यूवीव को इंटरफ़ेस बिल्डर में सेल में जोड़े गए लेबल के संबंध में रखने की कोशिश करता हूं।

- (id)initWithCoder:(NSCoder *)decoder 
{ 
    if ((self = [super initWithCoder:decoder])) 
    { 
     [self initSelf]; 
    } 
    return self; 
} 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) 
    { 
     [self initSelf]; 
    } 
    return self; 
} 

- (void) initSelf 
{ 
    // Initialization code 
    _badgeLabel = @""; 

    if (!_customBadge) 
    { 
     _customBadge = [CustomBadge customBadgeWithString:self.badgeLabel]; 
    } 

    // hide the badge until needed 
    self.customBadge.hidden = YES; 

    // add badge to the cell view hierarchy 
    [self.customBadge setTranslatesAutoresizingMaskIntoConstraints:NO]; 
    [self.customBadge setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal]; 
    [self.customBadge setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisVertical]; 

    [self.contentView addSubview:self.customBadge]; 
} 

अगर मैं initSelf के अंत कुछ नहीं होता पर बाधा कोड डाल:

यह कस्टम UIView बना सकते हैं और सेल की सामग्री को देखने के लिए इसे जोड़ने के लिए कोड है। मेरे _customBadge की स्थिति इसके 'डिफ़ॉल्ट पर रहता है। जब मैं लेआउट में बाधा कोड डालता हूं तो स्थिति को लागू किया जाता है; लेकिन मुझे पूरा यकीन है कि यह गलत जगह है। यहां कोड है:

- (void) layoutSubviews 
{ 
    [self.contentView addConstraint:[NSLayoutConstraint 
            constraintWithItem:self.customBadge 
            attribute:NSLayoutAttributeLeft 
            relatedBy:NSLayoutRelationEqual 
            toItem:self.competence 
            attribute:NSLayoutAttributeRight 
            multiplier:1.0 
            constant:-14.0]]; 

    [self.contentView addConstraint:[NSLayoutConstraint 
            constraintWithItem:self.customBadge 
            attribute:NSLayoutAttributeTop 
            relatedBy:NSLayoutRelationEqual 
            toItem:self.competence 
            attribute:NSLayoutAttributeTop 
            multiplier:1.0 
            constant:0.0]]; 

    [super layoutSubviews]; 
} 

क्या कोई मुझे बता सकता है कि यह कोड कहां जाना चाहिए? निश्चित रूप से जब भी कोई लेआउट होता है तो मैं डुप्लिकेट बाधाएं बना रहा हूं।

धन्यवाद

उत्तर

31

आप की तरह की कमी को अद्यतन करने की आवश्यकता होगी:

-(void)updateConstraints{ 
// add your constraints if not already there 
[super updateConstraints]; 
} 

superview करने के लिए अपने विचारों को जोड़ने के बाद आप [self setNeedsUpdateConstraints] कॉल करने के लिए उनका उपयोग शुरू करने की जरूरत है। ऐसा करके प्रतिपादन रनटाइम सही समय पर updateConstraints पर कॉल करेगा।

+1

प्रलेखन (https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/occ/instm/UIView/updateConstraints) कहते हैं, "कॉल करें [सुपर अपडेट कॉन्स्ट्रेन] आपके कार्यान्वयन में अंतिम चरण के रूप में। " – cnotethegr8

+26

लेकिन क्या यह हर बार अपडेट करने के लिए समान बाधाओं को दोबारा जोड़ता है? क्या पहले किसी भी बाधा को दोबारा जोड़ने से पहले इसे जोड़ना सबसे अच्छा अभ्यास है? या आईओएस बुद्धिमान यह पहचानने के लिए पर्याप्त है कि आप जो बाधा जोड़ रहे हैं वह पहले से मौजूद है और इसलिए इसे फिर से जोड़ना नहीं है? – PapillonUK

+0

मुझे लगता है कि किसी को सेट किए गए सेट अपडेट्स कॉन्टेस्टेंट्स को केवल कॉल नहीं करना चाहिए ... – Adrian

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