【问题标题】:IOS centered UIImages in a rowIOS居中UIImages连续
【发布时间】:2013-02-28 01:17:45
【问题描述】:

我有一个小问题。我想在 UITableViewCell(在中心)中显示多个(数量是动态的)UIImages。

我该怎么做(UIImages 是通过编程方式生成的)?

非常感谢!

【问题讨论】:

    标签: ios uiimage center tableviewcell


    【解决方案1】:
        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if(cell == nil)
            {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
                self.imgThings = [[UIImageView alloc] init];
               // imgThings.backgroundColor= [UIColor magentaColor];
                [self.imgThings setImage: [UIImage imageNamed:@"image.png"]];
                self.imgThings.center = CGPointMake(cell.contentView.bounds.size.width/2,cell.contentView.bounds.size.height/2);
                [cell.contentView addSubview:self.imgThings];
            }
      return cell;
    
    }
    

    以上只是一张图片的简单示例,您也可以通过更改图片名称来设置多张图片。

    【讨论】:

    • 您好,感谢您的 sn-p。这几乎是我需要的。但是在我的示例中,我有多个图像,而不仅仅是一行中的一个。例如这是一行: (UIImage) | (UIImage) | (UIImage) -- (DetailEnclosure) ;就像一张单行多列的表格。
    • @Bene 如果超过 3 张图片怎么办?
    • @Bene 如果您的目标是 iOS 6.0 及更高版本,现在可能是学习自动布局的好时机。您可以将规则设置为具有最小填充和规则,以使所有 imageView 具有与单元格的 contentView 相同的高度和宽度。
    • 嗨,对不起,这必须在 IOS 5 上运行。但我自己找到了解决方案。您可以将所有 UIImageViews 放在一个 UIView 中,将 UIView 居中并根据图片数量和图片尺寸 + 每张图片之间的计算空间来计算宽度和高度以填满整个单元格。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-16
    • 2021-04-11
    • 1970-01-01
    • 2023-02-02
    • 1970-01-01
    • 2020-09-01
    相关资源
    最近更新 更多