UITableViewCell使我們經(jīng)常使用的颅和,在開發(fā)中我們經(jīng)常會通過自定義UITableViewCell來實(shí)現(xiàn)各種各樣酷炫的效果。其實(shí)Cell類本身已經(jīng)提供了很多使用的方法給開發(fā)人員扳剿,比如Delete,Insert铜犬,Move等等
1舞终、使用系統(tǒng)自定義的各種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
-
UITableViewCellStyleSubtitle
*UITableViewCellValue1
在UITableViewCell內(nèi)默認(rèn)是有contentview和accessoryView這兩個(gè)subview的,contentview中的subview根據(jù)不同的cell的style會使用不同的布局癣猾。contentview和其中的默認(rèn)subview會根據(jù)cell的編輯狀態(tài)出現(xiàn)的控件自動縮進(jìn)敛劝,自定義cell時(shí)可以把自定義控件添加在contentview中,也可以直接添加到cell中纷宇。
1夸盟、設(shè)置UITableViewCell的屬性
//cell的右邊輔助按鈕的樣式
cell.accessoryType = UITableViewCellAccessoryCheckmark;
//自定義cell右邊的輔助按鈕
cell.accessoryView = nil;
//自定義cell的背景
cell.backgroundView = nil;
//設(shè)置cell的contentview中的detail的文字內(nèi)容
cell.detailTextLabel.text = @"";
//查看cell當(dāng)前的編輯模式
int style = cell.editingStyle;
//設(shè)置當(dāng)cell進(jìn)入編輯模式時(shí)的輔助按鈕樣式
cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
//自定義cell進(jìn)入編輯模式后輔助按鈕
cell.editingAccessoryView = nil;
//獲取cell的縮進(jìn)級別
int level = cell.indentationLevel;
//獲取cell的縮進(jìn)寬度
float width = cell.indentationWidth;
//設(shè)置cell被選中時(shí)的背景
cell.selectedBackgroundView = nil;
//設(shè)置cell的選中狀態(tài)樣式
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
//設(shè)置cell的contentview中的textlabel文字內(nèi)容
cell.textLabel.text = @"";
3、自定義的UITableViewCell重寫父類的方法
//初始化uitableviewcell后像捶,自定義cell添加subview
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
//當(dāng)cell被選中時(shí)上陕,uitableview內(nèi)部會自動調(diào)用該方法桩砰,重寫該方法可以在cell被選中時(shí)做一些額外的操作 - (void)setSelected:(BOOL)selected animated:(BOOL)animated
//當(dāng)cell處于高亮狀態(tài)時(shí),uitableview內(nèi)部會自動調(diào)用該方法释簿,重寫該方法可以在cell處于高亮?xí)r做一些額外操作
-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
//重寫layoutsubviews方法亚隅,為了查看當(dāng)cell改變編輯狀態(tài)時(shí),有什么subview
-(void)layoutSubviews{
[super layoutSubviews];
NSArray* subs = self.subviews;
for (UIView* sub in subs) {
NSLog(@"view:%@",sub);
}
}
當(dāng)進(jìn)入刪除編輯模式時(shí)庶溶,cell的subview有一個(gè)叫UITableViewCellDeleteConfirmationControl的subview煮纵,這代表刪除按鈕∑荩可以修改該view達(dá)到修改刪除按鈕的位置行疏,大小等屬性。
當(dāng)進(jìn)入移動編輯模式時(shí)套像,cell的subview有一個(gè)叫UITableViewCellReorderControl的subview酿联,這個(gè)代表移動按鈕《峁可以修改該view達(dá)到修改移動按鈕的位置贞让,大小等屬性。
當(dāng)進(jìn)入插入編輯模式時(shí)劲够,cell的subview有一個(gè)叫UITableViewCellEditControl的subview震桶,這個(gè)代表添加按鈕休傍≌饕铮可以修改該view達(dá)到修改添加按鈕的位置,大小等屬性磨取。
通過重寫一些方法實(shí)現(xiàn)自定義
//當(dāng)cell的狀態(tài)變?yōu)榫庉嫊r(shí)人柿,uitableview內(nèi)部會自動調(diào)用該方法,重寫該方法可以改變cell的布局
-(void)willTransitionToState:(UITableViewCellStateMask)state{
[super willTransitionToState:state];
}
//當(dāng)cell的狀態(tài)變?yōu)榫庉嫊r(shí)忙厌,uitableview內(nèi)部會自動調(diào)用該方法凫岖,重寫該方法可以改變cell的布局
-(void)didTransitionToState:(UITableViewCellStateMask)state{
[super didTransitionToState:state];
//滑動出現(xiàn)的刪除按鈕state是2的,編輯狀態(tài)下的刪除按鈕state是3的
if (state == UITableViewCellStateShowingDeleteConfirmationMask||state==3) {
for (UIView *subview in self.subviews) {
//cell的subview為UITableViewCellDeleteConfirmationControl時(shí)逢净,代表是刪除按鈕
if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) {
UIView *deleteButtonView = subview;
CGRect f = deleteButtonView.frame;
f.origin.x -= 50;
deleteButtonView.frame = f; }
}
}//插入和移動的編輯狀態(tài)state都是1
else if(state==UITableViewCellStateShowingEditControlMask){
for (UIView *subview in self.subviews) {
NSString* type = @"";
//判斷如果cell當(dāng)前是插入模式哥放,則尋找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:
創(chuàng)建一個(gè)uiview,設(shè)置它的backgroundcolor后再添加到cell里
方法3:
通過在- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath的回調(diào)中設(shè)置cell的backgroundcolor