首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITableViewCell不显示subViews

UITableViewCell不显示subViews
EN

Stack Overflow用户
提问于 2013-11-02 16:43:39
回答 2查看 1.5K关注 0票数 0

我使用下面的代码显示自定义UITableViewCell。然而,问题在于它只显示了backImage,而没有显示任何其他标签或图像。

这是密码和ss。

怎么了?

代码语言:javascript
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIImageView *maleIcon;
    UIImageView *femaleIcon;
    UIImageView *backImage;

    UILabel *name;
    UILabel *address;
    UILabel *maleCount;
    UILabel *femaleCount;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

    VenueClass *venue = [self.venueArray objectAtIndex:[indexPath row]];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyIdentifier"];
        backImage =  [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];

        maleIcon   =  [[UIImageView alloc] initWithFrame:CGRectMake(234,143,25,25)];
        femaleIcon =  [[UIImageView alloc] initWithFrame:CGRectMake(234,173,25,25)];

        maleIcon.image    =   [UIImage imageNamed:@"rectangle.png"];
        femaleIcon.image  =   [UIImage imageNamed:@"rectangle.png"];

        name        = [[UILabel alloc] initWithFrame:CGRectMake(10,148,140,22)];
        address     = [[UILabel alloc] initWithFrame:CGRectMake(10,178,140,22)];
        maleCount   = [[UILabel alloc] initWithFrame:CGRectMake(266,145,48,22)];
        femaleCount = [[UILabel alloc] initWithFrame:CGRectMake(266,175,48,22)];

        name.font        = [UIFont boldSystemFontOfSize:16];
        address.font     = [UIFont systemFontOfSize:14];
        maleCount.font   = [UIFont boldSystemFontOfSize:14];
        femaleCount.font = [UIFont boldSystemFontOfSize:14];

        name.backgroundColor        = [UIColor clearColor];
        address.backgroundColor     = [UIColor clearColor];
        maleCount.backgroundColor   = [UIColor clearColor];
        femaleCount.backgroundColor = [UIColor clearColor];

        name.textColor        = [UIColor whiteColor];
        address.textColor     = [UIColor whiteColor];
        maleCount.textColor   = [UIColor whiteColor];
        femaleCount.textColor = [UIColor whiteColor];

        [cell.contentView addSubview:backImage];
        [cell.contentView addSubview:name];
        [cell.contentView addSubview:address];
        [cell.contentView addSubview:maleCount];
        [cell.contentView addSubview:femaleCount];
        [cell.contentView addSubview:femaleIcon];
        [cell.contentView addSubview:maleIcon];

    }

    CLLocation *location = [[CLLocation alloc] initWithLatitude:[venue.latitude doubleValue] longitude:[venue.longitude doubleValue]];

    AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
    int distance = [location distanceFromLocation:appDelegate.myLocationCtrl.locationManager.location];
    distance = distance / 1000;

    [femaleCount setText:venue.femaleCount];
    [maleCount   setText:venue.maleCount];
    [name        setText:venue.name];
    [address     setText:[NSString stringWithFormat:@"%@/%d Km", venue.address, distance]];

    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.backgroundColor = [UIColor clearColor];


    NSString *path = [[NSString stringWithFormat:@"%@%@" ,IMAGE_URL, venue.imagePath ] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    [backImage setImageWithURL:[NSURL URLWithString:path] placeholderImage:[UIImage imageNamed:@""]
                  completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)
     {         
     }];

    return cell;
}

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-11-02 17:03:11

是的,你的背影太宽太高了,你把它设为320x100,试试60x100

对不起编辑:您的其他项目是关闭屏幕/单元格,因为它们是Y坐标>100。Tr将第二个参数简化为initWithFrame:CGRectMake(X,Y必须小于单元格高度、宽度、高度)

票数 1
EN

Stack Overflow用户

发布于 2013-11-02 17:02:46

您以前在代码中为标识符MyIdentifier注册了类吗?

如果是这样的话,那么cell永远不会为零,因为dequeueReusableCellWithIdentifier:方法总是返回一个单元格。从医生那里:

如果您为指定的标识符注册了一个类,并且必须创建一个新的单元格,则此方法通过调用该单元格的initWithStyle:重用标识符:方法来初始化该单元格。对于基于nib的单元格,此方法从提供的nib文件加载单元格对象。如果现有单元可以重用,则此方法将调用单元的prepareForReuse方法。

这意味着您的代码将永远不会运行您在if(cell == nil){}语句中执行的设置

如果您没有为该重用标识符注册一个Class或Nib,则应用程序应该会崩溃,因此我怀疑您在应用程序中看到的是您注册的默认的nib或类。

一种常见的方法是在自定义UITableViewCell类或nib中配置所有这些子标签和imageView,然后在这个tableView:cellForRowAtIndexPath:方法中为这些属性分配相关的值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19744247

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档