2012-10-17 61 views
0

大家好,我定製單元中,我已經UIImageViewUILabel我要分配圖像到UIImageView和文字UILabel這是在陣列,請任何一個能幫助我嗎? ?在此先感謝Dispalying文字和標籤中的UITableView的自定義單元格

我嘗試下面的代碼

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

    } 
    cell.textLabel.text =[CurArray objectAtIndex:indexPath.row]; 


    NSString *cellIcon = [[self IconArray] objectAtIndex:indexPath.row]; 
    UIImage *cellIconimage = [UIImage imageNamed:cellIcon]; 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:cellIconimage]; 
    [imageView setFrame:CGRectMake(100.0, 30.0, 30.0, 30.0)]; 
    [imageView setContentMode:UIViewContentModeScaleAspectFill]; 
    [cell addSubview:imageView]; 

    return cell; 
} 

有了這個代碼,我可以將圖像創建的ImageView但我不能指定文本UILable

+0

粘貼代碼 – Rajneesh071

回答

1

這裏IBCustomCellProjectList是作爲UITableViewCell與XIB只要按照邏輯

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
      IBCustomCellProjectList *cell = (IBCustomCellProjectList *) [tableView dequeueReusableCellWithIdentifier:nil]; 
     // yourCustomeCell *cell = (yourCustomeCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];//custom cell 
     // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) 
     { 
      NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"IBCustomCellProjectList" owner:self options:nil]; 

      for (id currentObject in topLevelObjects){ 
       if ([currentObject isKindOfClass:[UITableViewCell class]]){ 
        cell = (IBCustomCellProjectList *) currentObject; 
        break; 
       } 
      } 
     } 

     // Configure the cell. 
     cell.cellLabel.text = [yourTextArray objectAtIndex:i]; 
     cell.cellImageView.image = [UIImage imageNamed:[yourImageArray objectAtIndex:i]]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     return cell; 
    } 

同樣看到這個波紋管教程的示例..

  1. Custom cell with advance control and design

  2. http://www.appcoda.com/customize-table-view-cells-for-uitableview/Customize tableview Cell

:)

0

使用此代碼。也許這是你想要的。您將不得不使用方法cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:[cell reuseIdentifier]]; 

if (cell == nil) 
{ 
    [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
    cell = customCell; 
    customCell = nil; 
} 

// Configure the cell. 
    cell.Label.text = [[NSNumber numberWithInt:indexPath.row+1]stringValue]; 
    cell.Label.textColor = [UIColor blackColor]; 
    cell.ImageView.image = [UIImage imageNamed:[CountryFlag objectAtIndex:indexPath.row]]; 
    cell.textLabel.textColor=[UIColor whiteColor]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    return cell; 
} 
0

使用下面的代碼:

-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
      UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"]; 

      const NSInteger IMAGE_VIEW_TAG=1001; 
      const NSInteger LABEL_TAG=1002; 
      UIImageView *imageView; 
      UILabel *lbl; 
      if(cell==nil) 
      { 
        cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"] autorelease]; 

        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; 

        imageView =[[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)] autorelease]; 
        imageView.tag=IMAGE_VIEW_TAG; 
        imageView.contentMode=UIViewContentModeScaleAspectFit; 
        [cell.contentView addSubview:imageView]; 
        lbl=[[UILabel alloc] initWithFrame:CGRectMake(100, 20, 160, 20)]; 
        lbl.font=[UIFont fontWithName:@"Helvetica" size:14.0]; 
        lbl.adjustsFontSizeToFitWidth=YES; 
        lbl.minimumFontSize=10.0; 
        lbl.textAlignment=UITextAlignmentLeft; 
        lbl.textColor=[UIColor colorWithRed:73.0/255.0 green:73.0/255.0 blue:73.0/255.0 alpha:1.0]; 
        lbl.backgroundColor=[UIColor clearColor]; 
        lbl.tag=LABEL_TAG; 
        [cell.contentView addSubview:lbl]; 
      } 


      imageView=(UIImageView*)[cell.contentView viewWithTag:IMAGE_VIEW_TAG]; 
      lbl=(UILabel*)[cell.contentView viewWithTag:LABEL_TAG]; 

      imageView.image=[UIImage imageNamed:@"image.png"]; 
      [email protected]"Your Text"; 
      return cell; 
    } 

    -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
      return 65.0; 
    }