UITableViewCell自定义

添加到选中cell中,每一个cell闪烁1秒
[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];
//}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值