// 返回cellhtml
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{app
static NSString *CellIdentifier = @"MyCell";ide
// 自定義cell函數
MyCell *cell = (MyCell *)[tableVie dequeueReusableCellWithIdentifier:CellIdentifier];佈局
if (cell == nil){學習
// 這種方式,將會查找響應的xib文件,將不會調用initWithStyle方法動畫
NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:niloptions:nil];atom
cell = [array objectAtIndex:0];url
}spa
- (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:messageBackgroundViewbelowSubview:self.textLabel];
self.selectionStyle = UITableViewCellSelectionStyleNone;
//定製Delete字符串,添加函數 返回要顯示的字符串
-(NSString *)tableView:(UITableView*)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"刪除";
}