为什么我的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点的问题已经解决了,我用错了图像变量来显示图像)

当选择,你可以告诉的事情出现了混乱

![在这里输入的形象描述] [3]

在IB我有80集在320wide细胞的尺寸高,并在indentity选项卡下,我已将类更改为CustomClass。我敢肯定,我忽略了一些微不足道的东西,但如果有人能够给我一个骨头,我会很感激。我已经解决了图像不显示在我想要的位置的问题,但仍然存在背景颜色不变,字体大小显示不同的问题,并且在选中单元格时,它与单元格分隔符重叠。

注:试图包括屏幕截图,但因为我是新的所以不会让我。我已经提供了一个链接,用于将所选单元格的图像与下面的分隔符重叠。

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

好像你不使用的UIImageView你的笔尖参考,但您使用的是电池的默认ImageView的属性。

+0

雅,即固定我的形象安置的问题,但我仍然不知道为什么选择在小区重叠的分隔符。或者为什么背景颜色不会改变。 – 2011-05-27 21:19:41

+0

确保笔尖中的标签具有清晰的背景色。或者尝试用CGRectMake()手动更改“bg”的框架以进一步调试。你在改变其他地方的细胞高度吗?所选视图似乎比最初的视图具有更大的帧。 – Desdenova 2011-05-27 22:31:06

+0

显然我把它设置在我程序中其他地方的另一个高度,我忘记了它(DUH):(感谢快速拍打头部,我还改变了标签的背景以清除背景颜色,但背景色仍然 – 2011-05-27 23:13:15

我想说,imageview框架和你给它的图像大小之间有一个不匹配。

您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); 
+0

感谢您的建议,在我尝试了您的代码之后,我注意到我已经将图像链接到了我创建的错误变量,所以不是显示在我告诉它的位置,而只是将它扔到那里。仍然不确定为什么选择单元格时会重叠分隔符。 – 2011-05-27 20:59:06