【问题标题】:Resize UIImageView in UITableViewCell , with textlabel dynamically在 UITableViewCell 中调整 UIImageView 的大小,动态使用 textlabel
【发布时间】:2013-07-14 20:12:10
【问题描述】:

我想为所有单元格设置一个尺寸的 UIimageview,但是 当单元格的大尺寸发生变化时,图像被改变我想要一个尺寸的 UIimage

     UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if(!cell){

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];

    }

   cell.imageView.image = [UIImage imageNamed:@"England.png"];
   cell.imageView.frame = CGRectMake( 2, 0, 25, 25 );
   cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
   UIFont * myfont = [UIFont fontWithName:@"Helvetica" size:13.0];
   cell.textLabel.text = [_data objectAtIndex:indexPath.row];
   cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
   cell.textLabel.numberOfLines = 0;
   cell.textLabel.textAlignment = UITextAlignmentCenter;
   cell.textLabel.font = myfont;
   cell.textLabel.textColor = [UIColor grayColor];

    return cell;



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * description = [_data objectAtIndex:indexPath.row];

    CGSize size = [description sizeWithFont:[UIFont boldSystemFontOfSize:14.0f] constrainedToSize:CGSizeMake(190,40) lineBreakMode:UILineBreakModeWordWrap];

        return size.height+50;

}

【问题讨论】:

    标签: uitableview uiimageview uilabel tableview


    【解决方案1】:

    这里是代码

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
    {
        NSString *text = [items objectAtIndex:[indexPath row]];
    
        CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
    
        CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
    
        CGFloat height = MAX(size.height, 44.0f);
    
        return height + (CELL_CONTENT_MARGIN * 5);
    }
    
    
    - (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell;
        UILabel *label = nil;
    
        cell = [tv dequeueReusableCellWithIdentifier:@"Cell"];
        if (cell == nil)
        {
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"] autorelease];
    
            label = [[UILabel alloc] initWithFrame:CGRectZero];
            [label setLineBreakMode:UILineBreakModeWordWrap];
            [label setMinimumFontSize:FONT_SIZE];
            [label setNumberOfLines:0];
            [label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
    
            [label setTag:1];
    
    
    
            [[cell contentView] addSubview:label];
    
        }
        NSString *text = [items objectAtIndex:[indexPath row]];
    
        CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
    
        CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
    
        if (!label)
            label = (UILabel*)[cell viewWithTag:1];
    
        [label setText:text];
        [label setFrame:CGRectMake(CELL_CONTENT_MARGIN-10, CELL_CONTENT_MARGIN+30, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
    
    
    
    
    
        UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(128,5, 32, 32)];
        imv.image=[UIImage imageNamed:@"England.png"];
        [cell.contentView addSubview:imv];
        [imv release];
    
    
        return cell;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-02
      • 1970-01-01
      • 2011-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多