2010-06-28 19 views
10

की छवि दृश्य की बाउंड बदलना मैं UITableViewCell की छवि दृश्य के अंदर विभिन्न आकार की छवियों को रखने की कोशिश कर रहा हूं। मुझे छवि डेटा asynch'ly मिलता है, छवि बनाएँ, imageView के सामग्री मोड सेट करें और अंत में छवि दृश्य की सीमा निर्धारित करें। लेकिन कोड मेरे द्वारा किए गए किसी भी बदलाव के लिए असंवेदनशील लगता है। मैं चाहता हूं कि छवियों को 75x75 क्षेत्र में केंद्रित किया जाए। मैंने इस उद्देश्य के लिए नीचे दिए गए कोड को लिखा हैUITableViewCell

UIImage* image = [UIImage imageWithData:data]; 
[holder.imageView setContentMode:UIViewContentModeCenter || UIViewContentModeRedraw]; 
[holder.imageView setImage:image]; 
[holder.imageView setBounds:CGRectMake(0,0,75,75)]; 
[holder.imageView setFrame:CGRectMake(0,0,75,75)]; 
[holder setNeedsLayout]; 

जहां धारक UITableViewCell है। परिणाम मुझे हमेशा मिलता है। सभी छवियों में 75px ऊंचाई और विभिन्न चौड़ाई है। क्या कोई मुझे इस समस्या को हल करने में मदद कर सकता है?

मुझे एहसास हुआ है कि सामग्री सेटिंग मोड और सीमा गुणों का उस कोड में कोई प्रभाव नहीं पड़ता है। मैं अंतिम पंक्ति के बाद एक NSLog जोड़ा गया है और नीचे के रूप में परिणाम मिल गया है:

NSLog(@"imageview:%@ bounds and contentMode:%@ %@",[holder imageView],[holder.imageView bounds],[holder.imageView contentMode]); 

imageView: < UIImageView: 0x39ab8a0; फ्रेम = (0 0; 75 75); अपारदर्शी = नहीं; userInteractionEnabled = नहीं; परत = < CALayer: 0x39a92b0 >> सीमा और contentMode: (शून्य) (शून्य)

फिर भी कोई समाधान नहीं

उत्तर

42

हो गया, मैं अंत में समाधान नहीं मिला, यह मेरे हालांकि = 3 घंटे) की लागत

समाधान बाध्य, फ्रेम की तरह गुणों को बदलने के लिए है, contentMode में - (शून्य) मेरे layoutSubviews कस्टम UITableViewCell कक्षा की थोड। "चाल" इस विधि में लेआउट कोड लिखना है, अन्यथा कोड का कोई प्रभाव नहीं पड़ता है।

नीचे कोड मेरे लिए काम किया था। यह तालिका के पंक्तियों को लंबवत गठबंधन बनाता है।

- (void)layoutSubviews { 
    [super layoutSubviews]; 
    self.imageView.bounds = CGRectMake(0,0,75,75); 
    self.imageView.frame = CGRectMake(0,0,75,75); 
    self.imageView.contentMode = UIViewContentModeScaleAspectFit; 

    CGRect tmpFrame = self.textLabel.frame; 
    tmpFrame.origin.x = 77; 
    self.textLabel.frame = tmpFrame; 

    tmpFrame = self.detailTextLabel.frame; 
    tmpFrame.origin.x = 77; 
    self.detailTextLabel.frame = tmpFrame; 

} 
+0

बमर, मैंने सोचा कि मुझे एक और UITableViewCell सबक्लास बनाने के लिए मजबूर होना पड़ सकता है ... मेरे लिए इसकी पुष्टि करने के लिए समय देने के लिए धन्यवाद! –

+1

मुझे इस बात से सहमत होना है कि यह अपेक्षा के अनुसार काम करने के लिए समय की भारी बर्बादी है। –

+0

हालांकि यह काम करता है, यह अभी भी एक हैक है। एक कस्टम यूआईटीबल व्यूसेल में निर्धारित बाधाओं से फ्रेम/सीमाओं को काम करने की उम्मीद करेगा। –

1

'UIViewContentModeScaleAspectFit' या 'UIViewContentModeScaleAspectFill' के लिए imageView की "contentMode" संपत्ति से बदल कर देखें

+1

धन्यवाद, मैं अपनी सिफारिश की कोशिश की है, लेकिन यह काम नहीं किया। मुझे एहसास हुआ कि जो भी मूल्य मैं सामग्री पर सेट करता हूं, मोड और सीमाओं का कोई प्रभाव नहीं पड़ता है। मैंने इसे दर्शाने के लिए अपनी पहली पोस्ट संपादित की है। –

2

"UIViewContentModeCenter || UIViewContentModeRedraw" 1. के बराबर है यह भी एक bitfield नहीं है। आप UIViewContentModeCenter चाहते हैं।

UITableViewCell.imageView सेल द्वारा प्रबंधित किया जाता है।

UIImageView * iv = [[[UIImageView alloc] initWithImage:image] autorelease]; 
iv.frame = (CGRect){{0,0},{75,75}}; 
iv.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin| UIViewAutoresizingFlexibleRightMargin; 
iv.contentMode = UIViewContentModeScaleAspectFit; 
[holder.contentView addSubview:iv]; 
+0

धन्यवाद! मैंने सामग्री दृश्य का उपयोग किये बिना समस्या हल की। मैं 2 दिनों के बाद उत्तर के रूप में प्रश्न चिह्नित करूंगा =) –

10

तो UITableViewCell के साथ समस्या यह है कि आपके पास है: आप कस्टम लेआउट चाहते हैं, (मैं आप "एक 75x75 क्षेत्र में केंद्रित" क्या मतलब अनुमान लगा रहा हूँ) contentView करने की दृष्टि से जोड़ने का प्रयास अंतर्निर्मित वस्तुओं के आकार पर कोई नियंत्रण नहीं (अर्थात् imageView, contentView, accessoryView, backgroundView)। जब तालिका बदलती है, तो आपकी अनुकूलन खत्म हो जाती है।

आप, के रूप में Behlul ने बताया, आकार layoutSubviews का उपयोग करके सही होने के लिए मजबूर कर सकते हैं, लेकिन उस के साथ समस्या यह है कि layoutSubviews हर बार तालिका स्क्रॉल कहा जाता है। यह बहुत अनावश्यक री-लेआउट कॉल है।

एक वैकल्पिक, विधि contentView को सभी अपनी सामग्री के जोड़ना है।इसी प्रकार यदि आप पृष्ठभूमि को अनुकूलित कर रहे हैं, आप एक पारदर्शी backgroundView बना सकते हैं और की एक subview backgroundView के रूप में अपने कस्टम पृष्ठभूमि दृश्य (जैसे myBackgroundView) जोड़ सकते हैं।

इस तरह आप अपनी वस्तुओं को कैसे रख सकते हैं और आकार दे सकते हैं।

नीचे की तरफ स्टॉक संदेश एक्सेसरी या छवि दृश्यों से प्राप्त नहीं हुए हैं। आपको बस अपना खुद का बनाना है।

आशा है कि मदद करता है!

// This code is not tested 
// MyCustomTableViewCell 
- (id) init{ 
    self = [super initWithStyle: UITableViewCellStyleDefault reuseIdentifier:@"MyReuseIdentifier"]; 
    if(self){ 
     //image view 
     my_image_view = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"default_image.png"]] retain]; 
     [my_image_view setFrame:CGRectMake(10,10,30,30)]; 
     [self.contentView addSubview:my_image_view]; 

     //labels 
     my_text_label = [[[UILabel alloc] initWithFrame:CGRectMake(50,10,100,15)] retain]; 
     [self.contentView addSubview:my_text_label]; 
     //set font, etc 

     //detail label 
     my_detail_label = [[[UILabel alloc] initWithFrame:CGRectMake(50,25,100,15)] retain]; 
     [self.contentView addSubview:my_detail_label]; 
     //set font, etc 

     //accessory view 
     //Whatever you want to do here 
     //attach "accessoryButtonTapped" selector to button action 

     //background view 
     UIView* background_view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 50)] autorelease]; 
     [background_view setBackgroundColor:[UIColor greenColor]]; 
     background_view.layer.cornerRadius = 17; 
     background_view.layer.borderWidth = 3; 
     background_view.layer.borderColor = [UIColor whiteColor].CGColor; 

     [self setBackgroundView:[[[UIView alloc] init] autorelease]]; 
     [self.backgroundView addSubview:background_view]; 
    } 

    return self; 
} 

- (void) setLabelText: (NSString*) label_text{ 
    [my_text_label setText:label_text]; 
} 

- (void) setDetailText: (NSString*) detail_text{ 
    [my_detail_label setText: detail_text]; 
} 

- (void) accessoryButtonTapped{ 
    //call table view delegate's accessoryButtonTappedForRowWithIndexPath method 
} 
+0

यह वास्तव में चयनित उत्तर से बेहतर तरीका है। यदि आप चाहें तो आप एक xib के माध्यम से UITableViewCell भी बना सकते हैं। यहां एक ट्यूटोरियल है: http://ijoshsmith.com/2011/07/16/creating-a-custom-uitableviewcell-in-ios-4/ –

+0

यह मेरे लिए चाल है। –

0

UITableViewCell के उपवर्ग बनाएँ:

@interface UITableViewCellSubClass : UITableViewCell 

@end 

@implementation UITableViewCellSubClass 

- (void)layoutSubviews { 
    [super layoutSubviews]; 
    self.imageView.frame = CGRectMake(0,4,32,32); 
    self.textLabel.frame = CGRectMake(42,4,300,32); 
} 
@end