2012-10-17 2 views
0

Привет всем Я пользовательские элементы, в которых я имею UIImageView и UILabel я хочу назначить изображение для 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; 
    } 
Смежные вопросы