微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么我的CustomCell类无法正确显示?

如何解决为什么我的CustomCell类无法正确显示?

| 大家好,我正在尝试创建一个业务查找器样式的应用程序,并希望创建一个自定义单元以向用户显示一些信息。我有一个单元格来显示我要提供的所有信息,但是格式已关闭。这是我用来创建单元格的代码
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static Nsstring *CellIdentifier = @\"CustomCell\";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) 
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@\"CustomCellView\" owner:self options:nil];

#ifdef __IPHONE_2_1
        cell = (CustomCell *)[nib objectAtIndex:0];
#else
        cell = (CustomCell *)[nib objectAtIndex:1];
#endif
    }

    // Configure the cell.
    NSDictionary *dict = [rows objectAtIndex: indexPath.row];

    cell.bizNameLabel.text = [dict objectForKey:@\"name\"];
    cell.addressLabel.text = [dict objectForKey:@\"address\"];

    NSMutableString *detailed = [NSMutableString string];
    [detailed appendString:[dict objectForKey:@\"distance\"]];
    [detailed appendString:@\"mi\"];
    cell.mileageLabel.text = detailed;

    // determine the rating for the business
    Nsstring *icontype = [dict objectForKey:@\"rating\"];
    NSInteger rating = [icontype intValue];
    UIImage *image;
    // variable to change the color of the cell\'s background
    UIView *bg = [[UIView alloc] initWithFrame:cell.frame];

    // switch statement to determine the rating image to be displayed
    switch (rating) {
        case 1:
        {
            image = [UIImage imageNamed:@\"1.png\"];
            bg.backgroundColor = [UIColor yellowColor]; 
            break;
        }
        case 3:
        {
            image = [UIImage imageNamed:@\"3.png\"];
            bg.backgroundColor = [UIColor orangeColor]; 
            break;
        }
        case 5:
        {
            image = [UIImage imageNamed:@\"5.png\"];
            //bg.backgroundColor = [UIColor redColor]; 
            cell.backgroundColor = [UIColor redColor];
            break;
        }
        default:
            break;
    }

    [cell.ratingImage setimage:image];
    cell.backgroundView = bg;
    [bg release];


#ifdef __IPHONE_3_0
    cell.accessoryType =  UITableViewCellAccessorydisclosureIndicator;
#endif

    return cell;
}
然后,我在IB中创建了布局,如下所示。 (前2个问题已解决,我使用了错误的image变量来显示图像) 并且当您被选中时,您可以告诉您一切都不正常 ![在此处输入图片描述] [3] 在IB中,我将单元格的尺寸设置为320宽乘80high,并在“标识”选项卡下,将类更改为CustomClass。我确定我忽略了一些琐碎的事情,但是如果有人可以给我扔骨头,我将不胜感激。我已经解决了我的图像无法显示在我想要的位置上的问题,但是我仍然遇到背景颜色未更改,字体大小显示不同以及在选择单元格时重叠的问题。单元格分隔符。 注意:尝试添加屏幕截图,但是由于我是新手,所以不允许我这样做。我提供了一个链接,在其中放置了与下面的分隔符重叠的所选单元格的图像。 http://s1216.photobucket.com/albums/dd361/cabearsfan/?action=view¤t=screenshot2.png     

解决方法

        似乎您没有使用UIImageView的笔尖引用,而是使用了单元格的默认imageView属性。     ,        我想说的是imageview框架与您提供的图像尺寸不匹配。 在setImage调用之后,添加以下代码以了解发生了什么:
NSLog(@\" imageView frame %f,%f,%f\",imageView.frame.origin.x,imageView.frame.origin.y,imageView.frame.size.width,imageView.frame.size.height);
NSLog(@\" image  size %f x %f\",image.size.width,image.size.height);
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。