直接上转载代码吧!
// UITableViewCellExt.h @interface UITableViewCell (UITableViewCellEx) - (void)setBackgroundImage:(UIImage *)p_w_picpath; - (void)setBackgroundImageByName:(NSString *)p_w_picpathName; @end
#import "UITableViewCellExt.h" @implementation UITableViewCell (UITableViewCellExt) - (void)setBackgroundImage:(UIImage*)p_w_picpath { UIImageView *p_w_picpathView = [[UIImageView alloc] initWithImage:p_w_picpath]; p_w_picpathView.contentMode = UIViewContentModeCenter; self.backgroundView = p_w_picpathView; [p_w_picpathView release]; } - (void)setBackgroundImageByName:(NSString *)p_w_picpathName { [self setBackgroundImage:[UIImage p_w_picpathNamed:p_w_picpathName]]; } @end
调用示例:
// Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; cell.selectionStyle = UITableViewCellSelectionStyleNone; [cell setBackgroundImageByName:@"text-background.png"]; } return cell; }