UITabelViewCell自定义(zhuan)
// 返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"MyCell";
// 自定义cell
MyCell *cell = (MyCell *)[tableVie dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
// 这种方式,将会查找响应的xib文件,将不会调用initWithStyle方法
NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:nil options:nil];
cell = [array objectAtIndex:0];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"MyCell";
// 自定义cell
MyCell *cell = (MyCell *)[tableVie dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
// 这种方式,将会调用cell中的initWithStyle方法
cell = [[[MyCell alloc] initWithStyle:UITableViewCellSelectionStyleGray reuseIdentifier:CellIdentifier] autorelease];
}
return cell;
}
cell.accessoryType = UITableViewCellAccessoryNone;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0.0, 0.0, 55, 57)];
[button setImage:[UIImage imageNamed:@"tap_normal.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"tap_highlight.png"] forState:UIControlStateHighlighted];
[button setTag:indexPath.row];
[button addTarget:self action:@selector(doClickPlaybillAction:event:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundColor:[UIColor clearColor]];
[cell setAccessoryView:button];
return cell;
@property(nonatomic) UITableViewCellAccessoryType accessoryType;
@property(nonatomic,retain) UIView *accessoryView;
@property(nonatomic) UITableViewCellAccessoryType editingAccessoryType;
@property(nonatomic,retain) UIView *editingAccessoryView;
- (void) performExpand:(id)paramSender{
UITableViewCell *ownerCell = (UITableViewCell*)[paramSender superview];// 获得父视图,即TableViewCell
if (ownerCell != nil){
NSIndexPath *ownerCellIndexPath = [self.myTableView indexPathForCell:ownerCell];
NSLog(@"Accessory in index path is tapped. Index path = %@", ownerCellIndexPath);
}
}
通过,上面一步,我们为Cell添加了一个自定义的按钮。
也许就会遇到这么一个纠结的情况,当点击UITableViewCell高亮时,其子视图中不该高亮的对象(比如说自定义的那个按钮)也高亮了。
比如:
正确方式:我们需要cell被选中时,按钮不应该也被高亮显示。如:
错误方式:但是,cell被选中时,按钮却也高亮显示了。如:
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
[super setHighlighted:highlighted animated:animated];
if(highlighted) {
[(UIButton *)self.accessoryView setHighlighted:NO];
}
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated{
[super setSelected:selected animated:animated];
if(selected) {
[(UIButton *)self.accessoryView setHighlighted:NO];
}
}
因为当UITableViewCell为选中状态时,UITableViewCell把selectedBackgroundView当作一个子视图来添加;
selectedBackgroundView被添加在UITableViewCell的backgroundView之上,或者所有其它视图之下。
当调用setSelected: animated:这一方法时,会导致selectedBackgroundView以一个alpha消化的状态来出现和消失。
还应该注意:
UITableViewCell的selectionStyle值为UITableViewCellSelectionStyleNone时,selectedBackgroundView将不起作用。
// 设置背景
UIImageView *bgImage=[[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 57)] autorelease];
[bgImage setImage: [UIImage imageNamed:@"table_live_bg.png"]];
[self setBackgroundView:bgImage];
[self setBackgroundImageByName:@"table_live_bg.png"];
[self setBackgroundImage:[UIImage imageNamed:@"table_live_bg.png"]];
[self.contentView insertSubview:messageBackgroundView belowSubview:self.textLabel];
self.selectionStyle = UITableViewCellSelectionStyleNone;
//定制Delete字符串,添加函数 返回要显示的字符串
-(NSString *)tableView:(UITableView*)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"删除";
}