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

发布于 2024-11-10 07:38:14 字数 2874 浏览 4 评论 0原文

大家好,我正在尝试创建一个业务查找器风格的应用程序,并希望创建一个自定义单元格来向用户显示一些信息。我有单元格来显示我试图提供的所有信息,但格式已关闭。这是我用来创建单元格的代码。

// 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 中创建了如下所示的布局。

(前两个问题已解决,我使用了错误的图像变量来显示图像)

并且当选择时,您可以看出事情不正常

![输入图像描述此处][3]

在 IB 中,我将单元格的尺寸设置为 320 宽 x 80 高,并且在“缩进”选项卡下,我已将类更改为 CustomClass。我确信我忽略了一些微不足道的事情,但如果有人能给我一根骨头,我将不胜感激。我已经解决了图像未显示在我想要的位置的问题,但我仍然遇到背景颜色未更改、字体大小显示不同以及选择单元格时,它与单元格分隔符重叠的问题。

注意:尝试包含屏幕截图,但由于我是新人,所以不允许我这样做。我提供了一个链接,其中放置了与下面的分隔符重叠的所选单元格的图像。

http://s1216.photobucket.com/albums/ dd361/cabearsfan/?action=view&current=screenshot2.png

Hey all, I'm trying to create a business finder style app and was hoping to create a custom cell to display some information to the user. I've got the cell to display all the information I'm trying to give, but the format is off. Here is the code I'm using to create the cell.

// 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;
}

I then created the layout in IB to look like this..

(First 2 issues have been resolved, I was using the wrong image variable to display the image)

And when selected, you can tell things are out of whack

![enter image description here][3]

In IB I have the dimensions of the cell set at 320wide by 80high, and under the indentity tab, I've changed the class to CustomClass. I'm sure I'm overlooking something trivial, but if someone could throw me a bone, I'd be grateful. I've fixed the problem I was having with the image not displaying where I wanted it to, but I'm still having issues with the background color not changing, font size displaying different and when the cell is selected, it overlaps the cell separator.

Note: tried to include screen shots, but since I'm new SO wouldn't let me. I've provided a link where I've put the image of the selected cell that overlaps the separator below.

http://s1216.photobucket.com/albums/dd361/cabearsfan/?action=view¤t=screenshot2.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迟到的我 2024-11-17 07:38:14

似乎您没有使用 UIImageView 的 nib 引用,但您正在使用单元格的默认 imageView 属性。

Seems like you are not using your nib reference of the UIImageView but you are using the default imageView property of the cell.

牵你的手,一向走下去 2024-11-17 07:38:14

我想说图像视图框架和您提供的图像大小之间不匹配。

在 setImage 调用之后,添加以下代码以了解发生了什么:

NSLog(@" imageView frame %f,%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);

I'd say there is a mis-match between the imageview frame and the size of the image you're giving it.

After your setImage call, add this code to find out what's going on:

NSLog(@" imageView frame %f,%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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文