Customizing UITableViewCell
I'm attempting to customize a UITableViewCell to "pretty up" an app, and I'm running into trouble. My code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UIImage *rowBackground;
UIImag开发者_StackOverflowe *selectionBackground;
NSInteger sectionRows = [tableView numberOfRowsInSection:[indexPath section]];
NSInteger row = [indexPath row];
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if ( cell == nil ) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if (row == 0 && row == sectionRows - 1) {
rowBackground = [UIImage imageNamed:@"topAndBottomRow.png"];
selectionBackground = [UIImage imageNamed:@"topAndBottomRowSelected.png"];
NSLog(@"topAndBottom");
}
else if ( row == 0 ) {
rowBackground = [UIImage imageNamed:@"topRow.png"];
selectionBackground = [UIImage imageNamed:@"topRowSelected.png"];
NSLog(@"topRow");
}
else if ( row == sectionRows - 1 ) {
rowBackground = [UIImage imageNamed:@"bottomRow.png"];
selectionBackground = [UIImage imageNamed:@"bottomRowSelected.png"];
NSLog(@"bottomRow");
} else {
rowBackground = [UIImage imageNamed:@"middleRow.png"];
selectionBackground = [UIImage imageNamed:@"middleRowSelected.png"];
NSLog(@"middleRow");
}
((UIImageView *)cell.backgroundView).image = rowBackground;
((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;
NSString *cellValue = [[listOfItems objectAtIndex:indexPath.row] description];
cell.textLabel.text = cellValue;
return cell;
}
It selects the appropriate image, as shown in the console log, but I have two problems: 1. Setting the (cell.backgroundView).image has no effect 2. Setting the (cell.selectedBackgroundView).image errors out with: -[UITableViewCellSelectedBackground setImage:]: unrecognized selector sent to instance 0x4b90670
I can find hints about UIView issues with this in google, but haven't found anything about a UITableViewCell. Help?
There's some incompatibilities - there isn't an official background image you can set, IIRC, there's only a background UIView. If you want something that's a UIImageView, you have to do it yourelf.
So, instead, you have to manually set the cell's backgroundView to be a blank UIImageView *when you create the cell, e.g.:
cell.backgroundView = [[[UIImageView alloc] init] autorelease];
...which you can then set images upon.
精彩评论