2011-11-16 19 views
6

मैंने अस्थायी रूप से UITableViewCell प्रोग्रामेटिक रूप से कुछ मान जोड़े। लेकिन मुझे प्रत्येक सेल में छवियों को जोड़ने की जरूरत है। मैं उसे कैसे कर सकता हूँ?UITableView सेल में छवियों को कैसे जोड़ा जाए?

मेरा कोड यहां है। ज फाइल

@interface BidalertsViewController : UIViewController  <UITableViewDelegate,UITableViewDataSource> { 
    NSArray *listData; 
} 
@property(nonatomic, retain) NSArray *listData; 
@end 

मीटर फ़ाइल

@synthesize listData; 

- (void)viewDidLoad { 
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(5,0,310,28)]; 
newView.backgroundColor=[UIColor whiteColor]; 
UITextView *mytext = [[UITextView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 25.0)]; 
mytext.backgroundColor = [UIColor clearColor]; 
mytext.textColor = [UIColor blackColor]; 
mytext.editable = NO; 
mytext.font = [UIFont systemFontOfSize:15]; 
mytext.text = @"Asset Name"; 
[mytext release]; 
[newView addSubview:mytext]; 

[self.view addSubview:newView]; 
[newView release]; 


NSArray *array = [[NSArray alloc] initWithObjects:@"iPhone", @"iPod", @"iPad",nil]; 
self.listData = array; 
[array release]; 
[super viewDidLoad]; 
} 
- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
} 
- (void)dealloc { 
    [listData dealloc]; 
    [super dealloc]; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
return [self.listData count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier]; 
if (cell == nil) { cell = [[[UITableViewCell alloc] 
          initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease]; 
} 
NSUInteger row = [indexPath row]; 
cell.textLabel.text = [listData objectAtIndex:row]; 
return cell; 

UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)]; 
imv.image=[UIImage imageNamed:@"user.jpg"]; 
[cell.contentView addSubview:imv]; 
[imv release]; 
} 
@end 

कोड सेल करने के लिए छवियों को जोड़ नहीं था। समस्या क्या है? सेल में कस्टम छवि जोड़ने के लिए मैं कैसे कर सकता हूं? अपने - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath विधि में

cell.imageView.image = [UIImage imageNamed:@"user.jpg"]; 

:

उत्तर

12

ले जाएँ return cell;:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier]; 
    if (cell == nil) { cell = [[[UITableViewCell alloc] 
           initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease]; 
    } 
    NSUInteger row = [indexPath row]; 
    cell.textLabel.text = [listData objectAtIndex:row]; 


    UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)]; 
    imv.image=[UIImage imageNamed:@"user.jpg"]; 
    [cell.contentView addSubview:imv]; 
    [imv release]; 

    return cell; 
} 
+0

छवि प्रदर्शित होती है। लेकिन छवि पाठ के सामने प्रदर्शित होती है। पाठ पूरी तरह से नहीं दिखाया गया है। –

+3

यह ठीक से काम करता है क्योंकि 'टेक्स्ट लेबल' में फ्रेम है जो आपके 'UIImageView' के साथ छेड़छाड़ करता है। शायद आपको कस्टम सेल की आवश्यकता है। – beryllium

7

आप एक सेल के imageView संपत्ति का उपयोग कर छवियों को जोड़ सकते हैं।

+0

उत्तर के लिए धन्यवाद। यह काम नहीं कर रहा है –

+0

क्या आपके पास user.jpg नाम वाली छवि है? –

+0

हाँ मैंने संसाधन फ़ाइल में भी जोड़ा। –

0

यहां एक उदाहरण सेल है जो एक सेल सेल में एक फेसबुक छवि लोड कर रहा है। विधि के अंत में

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
             reuseIdentifier:CellIdentifier] autorelease]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 

    // Leave cells empty if there's no data yet 
    if (nodeCount > 0) 
    { 
     // Set up the cell... 
     AppRecord *appRecord = [self.friendsList objectAtIndex:indexPath.row]; 

     cell.textLabel.text = friend.name; 

     // Only load cached images; defer new downloads until scrolling ends 
     if (!appRecord.icon) 
     { 
      // set the url 
      appRecord.imageURLString = [NSString stringWithFormat:@"http://graph.facebook.com/%@/picture", friend.recordID]; 

      // request the download 
      if (self.tableView.dragging == NO && self.tableView.decelerating == NO) 
      { 
       [self startIconDownload:appRecord forIndexPath:indexPath]; 
      } 
      // if a download is deferred or in progress, return a placeholder image 
      cell.imageView.image = [UIImage imageNamed:@"Placeholder.png"];     
     } 
     else 
     { 
      cell.imageView.image = appRecord.icon; 
     } 

    } 

    return cell; 
+0

यह काम नहीं कर रहा है। –

0

आप इसे सही जोड़ने, लेकिन आप एक बात fogot:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier]; 
    if (cell == nil) { cell = [[[UITableViewCell alloc] 
           initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease]; 
    } 
    NSUInteger row = [indexPath row]; cell.textLabel.text = [listData objectAtIndex:row]; 

    UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)]; 
    imv.image=[UIImage imageNamed:@"user.jpg"]; 
    [cell.contentView addSubview:imv]; 
    [imv release]; 

    //you fogot this: 
    return cell; 
    } 
+0

यह काम नहीं करेगा क्योंकि आपके पास पहले से ही 'रिटर्न सेल' है;) – beryllium

+0

हाँ यह कोड काम नहीं कर रहा है –

+0

ओह ... इस लाइन के अंत में, हाँ। तो बस इसे प्रतिस्थापित करें। – SentineL

0

// छवि को देखने के लिए एक आयत बॉक्स बनाने

UIImageView *iconImage = [[UIImageView alloc] init]; 
iconImage.frame = CGRectMake(12,4,53.5,53.5); 

// Create a label for cells 
UILabel *cellName = [[UILabel alloc] initWithFrame:CGRectMake(75,18,200,20)]; 
cellName.font = [self fontForBodyTextStyle]; 

NSString *cellNameStr = [NSString stringWithFormat:@"%@",self.tableCellNames[indexPath.row]]; 

// Select path row tab actions 
switch (indexPath.row) { 
    case 0: 

     cellName.text = cellNameStr; 
     iconImage.image = [UIImage imageNamed:@"condition.png"]; 
     [cell.contentView addSubview:iconImage]; 
     [cell.contentView addSubview:cellName]; 

     break; 

    case 1: 
     cellName.text = cellNameStr; 
     iconImage.image = [UIImage imageNamed:@"videos.png"]; 
     [cell.contentView addSubview:iconImage]; 
     [cell.contentView addSubview:cellName]; 
     break; 

    case 2: 
     cellName.text = cellNameStr; 
     iconImage.image = [UIImage imageNamed:@"provider.png"]; 
     [cell.contentView addSubview:iconImage]; 
     [cell.contentView addSubview:cellName]; 
     break; 

    case 3: 
     cellName.text = cellNameStr; 
     iconImage.image = [UIImage imageNamed:@"info.png"]; 
     [cell.contentView addSubview:iconImage]; 
     [cell.contentView addSubview:cellName]; 
     break; 

    default: 
     NSAssert(NO, @"Unhandled value in cellForRowAtIndexPath"); 
     break; 
} 

धन्यवाद वीकेजे,

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