大家好,我正在尝试创建一个业务查找器风格的应用程序,并希望创建一个自定义单元格来向用户显示一些信息。我的单元格可以显示我要提供的所有信息,但格式已关闭。这是我用来创建单元格的代码。
// 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 中,我将单元格的尺寸设置为 320wide x 80high,在 indentity 选项卡下,我已将类更改为 CustomClass。我确定我忽略了一些微不足道的事情,但如果有人能给我一根骨头,我将不胜感激。我已经解决了图像未显示在我想要的位置时遇到的问题,但我仍然遇到背景颜色未更改、字体大小显示不同以及选择单元格时与单元格分隔符重叠的问题.
注意:试图包括屏幕截图,但由于我是新人所以不会让我这样做。我提供了一个链接,我在其中放置了与下面的分隔符重叠的所选单元格的图像。
http://s1216.photobucket.com/albums/dd361/cabearsfan/?action=view¤t=screenshot2.png