0

大家好,我有自定义单元格,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 分配文本

4

3 回答 3

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. 具有先进控制和设计的定制单元

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

:)

于 2012-10-17T05:33:42.090 回答
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;
}
于 2012-10-17T05:28:49.500 回答
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"];
          lbl.text=@"Your Text";
          return cell;
    }

    -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
             return 65.0;
    }
于 2012-10-17T11:37:47.123 回答