在UItableView的自定义单元格中显示文本和标签

时间:2012-10-17 05:23:52

标签: iphone ios5 xcode4

大家好我所有自定义单元格,我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;
}

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

3 个答案:

答案 0 :(得分: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

  3. :)

答案 1 :(得分: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;
}

答案 2 :(得分: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;
    }