[tableView deselectRowAtIndexPath:indexPath animated:YES];
设置cell背景色和背景图
UIView *backgrdView = [[UIView alloc] initWithFrame:cell.frame]; backgrdView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tabCell_background.png"]]; cell.backgroundView = backgrdView;
如果是自定义cell,需要注意
设置背景和组件
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code self.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tmall_bg_main"]]; self.logo = [[[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 70, 70)] autorelease]; self.logo.backgroundColor = [UIColor clearColor]; [self.contentView addSubview:self.logo]; self.title = [[[UILabel alloc] initWithFrame:CGRectMake(80, 20, 230, 20)] autorelease]; self.title.font = [UIFont systemFontOfSize:16.0f]; self.title.backgroundColor = [UIColor clearColor]; self.title.opaque = NO; [self.contentView addSubview:self.title]; self.subTtile = [[[UILabel alloc] initWithFrame:CGRectMake(80, 40, 230, 14)] autorelease]; self.subTtile.font = [UIFont systemFontOfSize:12.0f]; self.subTtile.textColor = [UIColor colorWithRed:158/255.0 green:158/255.0 blue:158/255.0 alpha:1.0]; self.subTtile.backgroundColor = [UIColor clearColor]; self.subTtile.opaque = NO; [self.contentView addSubview:self.subTtile]; UILabel *sLine1 = [[[UILabel alloc] initWithFrame:CGRectMake(0, 78, 320, 1)] autorelease]; sLine1.backgroundColor = [UIColor colorWithRed:198/255.0 green:198/255.0 blue:198/255.0 alpha:1.0]; UILabel *sLine2 = [[[UILabel alloc] initWithFrame:CGRectMake(0, 79, 320, 1)] autorelease]; sLine2.backgroundColor = [UIColor whiteColor]; [self.contentView addSubview:sLine1]; [self.contentView addSubview:sLine2]; } return self; }
在需要调用的地方(tableview初始化数据)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"cate_cell"; CateTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[CateTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; cell.selectionStyle = UITableViewCellSelectionStyleNone; } NSDictionary *cate = [self.cates objectAtIndex:indexPath.row]; cell.logo.image = [UIImage imageNamed:[[cate objectForKey:@"imageName"] stringByAppendingString:@".png"]]; cell.title.text = [cate objectForKey:@"name"]; NSMutableArray *subTitles = [[NSMutableArray alloc] init]; NSArray *subClass = [cate objectForKey:@"subClass"]; for (int i=0; i < MIN(4, subClass.count); i++) { [subTitles addObject:[[subClass objectAtIndex:i] objectForKey:@"name"]]; } cell.subTtile.text = [subTitles componentsJoinedByString:@"/"]; [subTitles release]; return cell; }
//如果自定义cell中没有initUI可以使用下列
// static NSString *customCellIdentifier = @"CustomCellIdentifier"; // UINib *nib = [UINib nibWithNibName:@"CustomAlarmCell" bundle:nil]; // [m_tabAlarmDateList registerNib:nib forCellReuseIdentifier:customCellIdentifier]; // // CustomAlarmCell *cell = [m_tabAlarmDateList dequeueReusableCellWithIdentifier:customCellIdentifier];
当在一个tableview中使用自定义cell,滑动时想更换cell的样式时,可以使用delegate
- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomAlarmCell *cell = (CustomAlarmCell*)[tableView cellForRowAtIndexPath:indexPath];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelay:.1f];
cell.m_switchAlarm.frame = CGRectMake(185, 17, 79, 27);
[UIView commitAnimations];
}
- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomAlarmCell *cell = (CustomAlarmCell*)[tableView cellForRowAtIndexPath:indexPath];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelay:.1f];
cell.m_switchAlarm.frame = CGRectMake(227, 17, 79, 27);
[UIView commitAnimations];
}
//- (void)willTransitionToState:(UITableViewCellStateMask)state
//{
// [super willTransitionToState:state];
// if(self.editingStyle == UITableViewCellEditingStyle)
// {
// m_switchAlarm.hidden = YES;
// }
//}
//- (void)didTransitionToState:(UITableViewCellStateMask)state
//{
// [super didTransitionToState:state];
//}