UITableViewCell使我們經常使用的,在開發中我們經常會通過自定義UITableViewCell來實現各種各樣酷炫的效果。其實Cell類本身已經提供了很多使用的方法給開發人員,比如Delete,Insert,Move等等
1、使用系統自定義的各種UITableViewCell的樣式
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID = @"CuustomCellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.textLabel.text = [NSString stringWithFormat:@"UITableViewCellStyleDefault --> %ld",(long)indexPath.row];
return cell;
}
通過加載Nib文件自定TabelViewCell
-(instancetype) initWithTableView:(UITableView *)tableView Title:(NSString *)title image:(NSString *)imgPath{
static NSString *cellID = @"LHSwitchTableViewCell";
[tableView registerNib:[UINib nibWithNibName:@"LHSwitchTableViewCell" bundle:nil] forCellReuseIdentifier:cellID];
LHSwitchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
cell.iconImageView.image = [UIImage imageWithContentsOfFile:[LZGlobal GetAlbumPrinterDocFilePath:imgPath]];
cell.titleLabel.text = title;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
TableViewCell有集中常用的樣式
-
UITableViewCellStyleDefault
TableViewCellStyleDefault.png -
UITableViewCellStyleSubtitle
TableViewCellStyleSubtitle.png
*UITableViewCellValue1
UITableViewCellStyleValue1.png
在UITableViewCell內默認是有contentview和accessoryView這兩個subview的,contentview中的subview根據不同的cell的style會使用不同的布局。contentview和其中的默認subview會根據cell的編輯狀態出現的控件自動縮進,自定義cell時可以把自定義控件添加在contentview中,也可以直接添加到cell中。
1、設置UITableViewCell的屬性
//cell的右邊輔助按鈕的樣式
cell.accessoryType = UITableViewCellAccessoryCheckmark;
//自定義cell右邊的輔助按鈕
cell.accessoryView = nil;
//自定義cell的背景
cell.backgroundView = nil;
//設置cell的contentview中的detail的文字內容
cell.detailTextLabel.text = @"";
//查看cell當前的編輯模式
int style = cell.editingStyle;
//設置當cell進入編輯模式時的輔助按鈕樣式
cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
//自定義cell進入編輯模式后輔助按鈕
cell.editingAccessoryView = nil;
//獲取cell的縮進級別
int level = cell.indentationLevel;
//獲取cell的縮進寬度
float width = cell.indentationWidth;
//設置cell被選中時的背景
cell.selectedBackgroundView = nil;
//設置cell的選中狀態樣式
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
//設置cell的contentview中的textlabel文字內容
cell.textLabel.text = @"";
3、自定義的UITableViewCell重寫父類的方法
//初始化uitableviewcell后,自定義cell添加subview
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
//當cell被選中時,uitableview內部會自動調用該方法,重寫該方法可以在cell被選中時做一些額外的操作 - (void)setSelected:(BOOL)selected animated:(BOOL)animated
//當cell處于高亮狀態時,uitableview內部會自動調用該方法,重寫該方法可以在cell處于高亮時做一些額外操作
-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
//重寫layoutsubviews方法,為了查看當cell改變編輯狀態時,有什么subview
-(void)layoutSubviews{
[super layoutSubviews];
NSArray* subs = self.subviews;
for (UIView* sub in subs) {
NSLog(@"view:%@",sub);
}
}
當進入刪除編輯模式時,cell的subview有一個叫UITableViewCellDeleteConfirmationControl的subview,這代表刪除按鈕。可以修改該view達到修改刪除按鈕的位置,大小等屬性。
當進入移動編輯模式時,cell的subview有一個叫UITableViewCellReorderControl的subview,這個代表移動按鈕。可以修改該view達到修改移動按鈕的位置,大小等屬性。
當進入插入編輯模式時,cell的subview有一個叫UITableViewCellEditControl的subview,這個代表添加按鈕。可以修改該view達到修改添加按鈕的位置,大小等屬性。
通過重寫一些方法實現自定義
//當cell的狀態變為編輯時,uitableview內部會自動調用該方法,重寫該方法可以改變cell的布局
-(void)willTransitionToState:(UITableViewCellStateMask)state{
[super willTransitionToState:state];
}
//當cell的狀態變為編輯時,uitableview內部會自動調用該方法,重寫該方法可以改變cell的布局
-(void)didTransitionToState:(UITableViewCellStateMask)state{
[super didTransitionToState:state];
//滑動出現的刪除按鈕state是2的,編輯狀態下的刪除按鈕state是3的
if (state == UITableViewCellStateShowingDeleteConfirmationMask||state==3) {
for (UIView *subview in self.subviews) {
//cell的subview為UITableViewCellDeleteConfirmationControl時,代表是刪除按鈕
if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) {
UIView *deleteButtonView = subview;
CGRect f = deleteButtonView.frame;
f.origin.x -= 50;
deleteButtonView.frame = f; }
}
}//插入和移動的編輯狀態state都是1
else if(state==UITableViewCellStateShowingEditControlMask){
for (UIView *subview in self.subviews) {
NSString* type = @"";
//判斷如果cell當前是插入模式,則尋找UITableViewCellEditControl的subview,代表添加按鈕
if (self.editingStyle==UITableViewCellEditingStyleInsert) {
type = @"UITableViewCellEditControl";
}
//否則尋找UITableViewCellReorderControl的subview,代表移動按鈕
else type = @"UITableViewCellReorderControl";
if ([NSStringFromClass([subview class]) isEqualToString:type]) {
UIView *deleteButtonView = [subview.subviews objectAtIndex:0];
CGRect f = deleteButtonView.frame;
f.origin.x -= 50;
deleteButtonView.frame = f;
}
}
}
}
4、UITableViewCell自定義背景顏色
方法1:
通過修改contentview的backgroundcolor
方法2:
創建一個uiview,設置它的backgroundcolor后再添加到cell里
方法3:
通過在- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath的回調中設置cell的backgroundcolor