【问题标题】:UITableView: How to offset text in cell?UITableView:如何偏移单元格中的文本?
【发布时间】:2011-03-06 14:12:52
【问题描述】:

您好,

我正在尝试使用异步加载图像 (80px x 60px) 来实现 tableview。我有异步位工作,但是我的文本 (cell.textlabel.text) 没有正确显示 - 它与图像重叠!

这是我 iPhone 的照片:

关于如何解决此问题的任何想法?

我尝试使用 CGRectMake 设置框架,但它不起作用...这里完全没有想法,我想问问 stackoverflow 社区。​​p>

非常感谢,

这是我正在使用的代码: -(UITableViewCell ) tableView:(UITableView)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

    static NSString *CellIdentifier = @"ImageCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc]
                 initWithFrame:CGRectZero reuseIdentifier:CellIdentifier]
                autorelease];
    } else {
        AsyncImageView* oldImage = (AsyncImageView*)
        [cell.contentView viewWithTag:999];
        [oldImage removeFromSuperview];
    }

    CGRect frame;
    frame.size.width=80; frame.size.height=60;
    frame.origin.x=0; frame.origin.y=0;
    AsyncImageView* asyncImage = [[[AsyncImageView alloc]
                                   initWithFrame:frame] autorelease];
    asyncImage.tag = 999;
    NSString *urlStr = [[NSString alloc] initWithFormat:@"http://www.example.com/driverPhotos/%@/%@_80.png",[options objectAtIndex:[indexPath row]],[images objectAtIndex:[indexPath row]]];
    NSURL* url=[NSURL URLWithString:urlStr];
    [asyncImage loadImageFromURL:url];

    [cell.contentView addSubview:asyncImage];

    cell.textLabel.frame=CGRectMake(100, 0, 220, 60);
    cell.textLabel.text=@"Hi";

    return cell;

}

【问题讨论】:

  • 您是否尝试将图像设置为 cell.imageView.image 属性 insted addSubview?
  • @Mat He 正在使用自定义图像视图。
  • 我正在使用子视图,因为我希望图像异步加载。

标签: iphone uitableview


【解决方案1】:

您不能更改任何默认子视图(detailTextLabel、textLabel、imageView 等)的框架,如果您真的想要,那么您必须继承 UITableView 并覆盖布局子视图。

我建议忽略 cell.textLabel 并创建自己的 UILabel 并将其添加到 cell.contentView,就像添加 AsyncImageView 一样(带有标签等)。

如果您没有在cell.textLabel 上设置任何内容,那么它就不会出现并掩盖您的AsyncImageView。 (但是,如果确实如此,那么只需将 AsyncImageView 带到前面)

顺便说一句,如果你同时按下 Home 键和锁定键,iPhone 会为你截屏并保存到相册中。您可以将其通过电子邮件发送给自己并在您的 PC 上获取。

【讨论】:

    【解决方案2】:

    或者您可以有一个自定义单元格并在layoutSubviews 中实现 x, y:

    -(void) layoutSubviews
    {
        [super layoutSubviews];
        [[self textLabel] setFrame:CGRectMake(75,10,250,20)];
        [[self detailTextLabel] setFrame:CGRectMake(75,30,250,20)];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-25
      • 2017-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多