为什么我的 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 中创建了如下所示的布局。
(前两个问题已解决,我使用了错误的图像变量来显示图像)
并且当选择时,您可以看出事情不正常
![输入图像描述此处][3]
在 IB 中,我将单元格的尺寸设置为 320 宽 x 80 高,并且在“缩进”选项卡下,我已将类更改为 CustomClass。我确信我忽略了一些微不足道的事情,但如果有人能给我一根骨头,我将不胜感激。我已经解决了图像未显示在我想要的位置的问题,但我仍然遇到背景颜色未更改、字体大小显示不同以及选择单元格时,它与单元格分隔符重叠的问题。
注意:尝试包含屏幕截图,但由于我是新人,所以不允许我这样做。我提供了一个链接,其中放置了与下面的分隔符重叠的所选单元格的图像。
http://s1216.photobucket.com/albums/ dd361/cabearsfan/?action=view¤t=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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
似乎您没有使用 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.
我想说图像视图框架和您提供的图像大小之间不匹配。
在 setImage 调用之后,添加以下代码以了解发生了什么:
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: