2012-02-25 15 views
5

जब तालिका सेल के लिए एक छवि को जोड़ने, डिफ़ॉल्ट के रूप में छोड़ दिया जाता है:UITableViewCell छवि संरेखण, संभव?

cell.imageView.image = [UIImage imageNamed:[arrImages objectAtIndex:indexPath.row]]; 

मैं इसे कैसे बदल सकते हैं कि UITableViewCell में हर छवि सही करने के लिए घटा देती जाना होगा और पाठलेबल बाईं ओर 10px हो जाएगा छवि के।

बहुत बहुत धन्यवाद!

+0

मैं डर लग रहा है आप के साथ standar 'नहीं कर सकते UITableViewCell' आपको अपने सेल का उपयोग करना होगा और फिर इसे अनुकूलित करना होगा। – HelmiB

उत्तर

10

एक और तरीका है एक कस्टम सेल बना सकते हैं और layoutSubviews विधि ओवरराइड करने के लिए है।

@interface CustomCell : UITableViewCell 

@end 

@implementation CustomCell 

- (void)layoutSubviews 
{ 
    [super layoutSubviews]; 

    // grab bound for contentView 
    CGRect contentViewBound = self.contentView.bounds; 
    // grab the frame for the imageView 
    CGRect imageViewFrame = self.imageView.frame; 
    // change x position 
    imageViewFrame.origin.x = contentViewBound.size.width - imageViewFrame.size.width; 
    // assign the new frame 
    self.imageView.frame = imageViewFrame; 
} 

@end 

कि Rembember cellForRowAtIndexPath में आप बना सकते हैं और CustomCell और नहीं UITableViewCell पुन: उपयोग करने की जरूरत है।

उम्मीद है कि यह मदद करता है।

संपादित

#import "CustomCell.h" 

// other code here... 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"CustomCell"; 
    CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    return cell; 
} 
+0

समाधान के लिए धन्यवाद। पीएस: imageViewFrame.position.x नहीं, लेकिन imageViewFrame.origin.x – uerceg

+0

ग्रेट समाधान! मैंने छवि – louissmr

+0

को केंद्रित करने के लिए अपना कोड उपयोग किया है इसका एक त्वरित संस्करण प्रयुक्त होता है और यह बहुत अच्छा काम करता है। – LondonGuy

1

इस प्रयास करें:

cell.imageView.frame = CGRectMake(cell.frame.size.width - cell.imageView.frame.size.width, cell.imageView.frame.origin.y, cell.imageView.frame.size.width, cell.imageView.frame.size.height); 
[cell.yourTexLabel sizeToFit]; 
cell.yourTexLabel.frame = CGRectMake(cell.imageView.origin.x - cell.yourTexLabel.frame.size.width - 10, cell.yourTexLabel.frame.origin.y, cell.yourTexLabel.frame.size.width, cell.yourTexLabel.frame.size.height); 
+0

कोई दोस्त नहीं ... यह काम नहीं कर रहा है। भी, 'yourTexLabel' 'cell.text लेबल है, लेकिन वह इसे किसी कारण से पहचान नहीं सकता .. –

+0

ओह ठीक है, दोस्त ... मैंने अनुमान लगाया है कि आप अपने सेल-सबक्लास का उपयोग कर रहे थे ... आप' मानक सेल के साथ बहुत कुछ नहीं करते ... – meronix

0

एक समाधान के लिए एक कस्टम UITableViewCell उपयोग करने के लिए है। कदम हैं:

  1. उदाहरण LabeledImageTableViewCell के लिए एक नया उद्देश्य-सी क्लास UITableViewCell का एक उपवर्ग है कि, बनाएँ। ivars और गुणUILabel और UIImageView के लिए घोषित करें।
  2. इंटरफ़ेस बिल्डर में, सामग्रीUITableView से गतिशील प्रोटोटाइप पर सेट करें। एक तालिका दृश्य सेल करने के लिए एक UIImageView और एक UILabel खींचें और उन्हें स्थिति। LabeledImageTableViewCell करने के लिए सेल की क्लास सेट करें। LabeledImageTableViewCell की UILabel & UIImageView वस्तुओं के लिए सेल के आउटलेट से कनेक्ट करें।
  3. UITableView के लिए प्रतिनिधि (आमतौर पर एक UITableViewController, कभी कभी एक UIViewController) को लागू डेटा स्रोत तरीकों में, उदाहरण के लिए:

    //#pragma mark - Table view data source 
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    { 
        return 1; 
    } 
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
        return (NSInteger)[rowData count]; 
    } 
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
        static NSString *CellIdentifier = @"tvcLabeledImage"; 
        LabeledImageTableViewCell *cell = (LabeledImageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
        if (cell == nil) { 
         cell = [[LNCCorrelationInfoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
        } 
        cell.myImage = [UIImage imageNamed:@"imageForThisRow.png"]; 
        cell.myLabel = "imageForThisRow"; 
        return cell; 
    } 
    

इसके अलावा, बाहर की जाँच WWDC 2011, से एप्पल वीडियो UITableView परिवर्तन, टिप्स & ट्रिक्स और इंटरफ़ेस बिल्डर स्टोरीबोर्डिंग परिचय (लॉग इन आवश्यक: https://developer.apple.com/videos/wwdc/2011/।)

5

समाधान यहाँ कोड का पता लगाएं।

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"foo.png"]]; 
cell.accessoryView = imageView; 

आपके संदर्भ के लिए।

UITableViewCell with image on the right?

+0

धन्यवाद नरसिम्हा नल्लासमेटी ;) – Abo3atef

1

मिले यहाँ https://stackoverflow.com/a/31616694/1884707

cell.contentView.transform = CGAffineTransformMakeScale(-1,1); 
cell.imageView.transform = CGAffineTransformMakeScale(-1,1); 
cell.textLabel.transform = CGAffineTransformMakeScale(-1,1); 
cell.textLabel.textAlignment = NSTextAlignmentRight; 

@TomSwift से एक बेहतर जवाब एक contentView आप पर बदलना सही पर imageView जगह कर सकते हैं लागू करके।

0
swift3 और swift4 में

, हम इस का उपयोग कर सकते हैं:

cell.accessoryView = UIImageView (छवि: UIImage (नाम: "imageNmae"))