【问题标题】:Dispalying text and label in custom cell of UItableView在 UItableView 的自定义单元格中显示文本和标签
【发布时间】:2012-10-17 05:23:52
【问题描述】:

大家好,我有自定义单元格,其中有UIImageViewUILabel 我想将图像分配给UIImageView,将文本分配给UILabel,它们在数组中,有人可以帮我吗?提前致谢

我试过下面的代码

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

使用此代码,我可以将图像分配给创建的图像视图,但我无法为 UILable 分配文本

【问题讨论】:

    标签: iphone ios5 xcode4


    【解决方案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

    :)

    【讨论】:

      【解决方案2】:

      使用此代码。也许这就是你想要的。您将不得不使用 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;
      }
      

      【讨论】:

        【解决方案3】:

        使用以下代码:

           -(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"];
                  lbl.text=@"Your Text";
                  return cell;
            }
        
            -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
            {
                     return 65.0;
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多