2011-11-16 40 views
6

Tôi tạm thời thêm một số giá trị vào UITableViewCell theo chương trình. Nhưng tôi cần phải thêm hình ảnh vào từng ô. Làm thế nào tôi có thể làm điều đó?Cách thêm hình ảnh vào ô UITableView?

Đây là mã của tôi. .h nộp

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

.m tập tin

@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 

Mã không được thêm hình ảnh vào các tế bào. Có chuyện gì vậy? Làm cách nào để thêm hình ảnh tùy chỉnh vào ô?

Trả lời

12

Move return cell; ở phần cuối của phương pháp:

- (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

được hiển thị. nhưng hình ảnh được hiển thị ở phía trước văn bản. văn bản không được hiển thị đầy đủ. –

+3

hoạt động bình thường vì 'textLabel' có khung giao cắt với' UIImageView' của bạn. Có lẽ bạn cần cho ô tùy chỉnh. – beryllium

7

Bạn có thể thêm hình ảnh bằng cách sử dụng tài sản IMAGExem của một tế bào:

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

trong phương pháp - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath của bạn.

+0

cảm ơn bạn đã trả lời. Nó không hoạt động –

+0

Bạn có một user.jpg tên là hình ảnh? –

+0

vâng tôi cũng đã thêm vào tệp tài nguyên. –

0

Đây là ô mẫu tải hình ảnh facebook trong ô bảng.

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

không hoạt động. Hình ảnh –

0

bạn thêm điều đó đúng, nhưng bạn fogot một điều:

- (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

nó sẽ không hoạt động vì bạn đã có 'return cell;' ở trên;) – beryllium

+0

yeah mã này không hoạt động –

+0

oh ... ở cuối dòng này, vâng. sau đó chỉ cần thay thế nó. – SentineL

0

// tạo một hộp hình chữ nhật để xem hình ảnh

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; 
} 

Cảm ơn VKJ,

Các vấn đề liên quan