Why isn't my CustomCell class displaying properly?
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
Seems like you are not using your nib reference of the UIImageView but you are using the default imageView property of the cell.
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);
精彩评论