NSArray*array1_=@[@"張鐵林",@"張國(guó)立",@"張國(guó)榮",@"張藝謀",@"張惠妹"];
NSArray*array2_=@[@"李小龍",@"李小路"];
NSArray*array3_=@[@"王剛"];
self.myDic=@{@"老張家":array1_,@"老李家":array2_,@"老王家":array3_};
UITableView*myTableView_=[[UITableViewalloc]initWithFrame:CGRectMake(0,0,320,460)style:UITableViewStylePlain];
myTableView_.delegate=self;
myTableView_.dataSource=self;
//改變換行線(xiàn)顏色
myTableView_.separatorColor= [UIColorblueColor];
//設(shè)定Header的高度扛稽,
myTableView_.sectionHeaderHeight=50;
//設(shè)定footer的高度智什,
myTableView_.sectionFooterHeight=100;
//設(shè)定行高
myTableView_.rowHeight=100;
//設(shè)定cell分行線(xiàn)的樣式橄镜,默認(rèn)為UITableViewCellSeparatorStyleSingleLine
[myTableView_setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
//設(shè)定cell分行線(xiàn)顏色
[myTableView_setSeparatorColor:[UIColorredColor]];
//編輯tableView
myTableView_.editing=NO;
[self.viewaddSubview:myTableView_];
//跳到指的row or section
[myTableView_scrollToRowAtIndexPath:[NSIndexPathindexPathForRow:2inSection:2]
atScrollPosition:UITableViewScrollPositionBottom animated:NO];
}
//指定有多少個(gè)分區(qū)(Section)容达,默認(rèn)為1
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {
return[[self.myDicallKeys]count];
}
//每個(gè)section底部標(biāo)題高度(實(shí)現(xiàn)這個(gè)代理方法后前面sectionHeaderHeight設(shè)定的高度無(wú)效)
-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section{
return20;
}
//每個(gè)section頭部標(biāo)題高度(實(shí)現(xiàn)這個(gè)代理方法后前面sectionFooterHeight設(shè)定的高度無(wú)效)
-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section{
return20;
}
//每個(gè)section頭部的標(biāo)題-Header
- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{
return[[self.myDicallKeys]objectAtIndex:section];
}
//每個(gè)section底部的標(biāo)題-Footer
- (NSString*)tableView:(UITableView*)tableView titleForFooterInSection:(NSInteger)section{
return nil;
}
//用以定制自定義的section頭部視圖-Header
-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section{
return nil;
}
//用以定制自定義的section底部視圖-Footer
-(UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section{
UIImageView*imageView_=[[UIImageViewalloc]initWithFrame:CGRectMake(0,0,320,20)];
imageView_.image=[UIImageimageNamed:@"1000.png"];
return[imageView_autorelease];
}
//指定每個(gè)分區(qū)中有多少行,默認(rèn)為1
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
return[[self.myDicobjectForKey:[[self.myDicallKeys]objectAtIndex:section]]count];
}
//改變行的高度(實(shí)現(xiàn)主個(gè)代理方法后rowHeight設(shè)定的高度無(wú)效)
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{
return100;
}
//繪制Cell
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
staticNSString*SimpleTableIdentifier =@"SimpleTableIdentifier";
UITableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
if(cell ==nil) {
cell = [[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier: SimpleTableIdentifier]autorelease];
//設(shè)定附加視圖
[cellsetAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
//UITableViewCellAccessoryNone,//沒(méi)有標(biāo)示
//UITableViewCellAccessoryDisclosureIndicator,//下一層標(biāo)示
//UITableViewCellAccessoryDetailDisclosureButton, //詳情button
//UITableViewCellAccessoryCheckmark//勾選標(biāo)記
//設(shè)定選中cell時(shí)的cell的背影顏色
cell.selectionStyle=UITableViewCellSelectionStyleBlue;//選中時(shí)藍(lán)色效果
//cell.selectionStyle=UITableViewCellSelectionStyleNone; //選中時(shí)沒(méi)有顏色效果
//cell.selectionStyle=UITableViewCellSelectionStyleGray;//選中時(shí)灰色效果
//
////自定義選中cell時(shí)的背景顏色
//UIView *selectedView = [[UIView alloc] initWithFrame:cell.contentView.frame];
//selectedView.backgroundColor = [UIColor orangeColor];
//cell.selectedBackgroundView = selectedView;
//[selectedView release];
//cell.selectionStyle=UITableViewCellAccessoryNone; //行不能被選中
}
//這是設(shè)置沒(méi)選中之前的背景顏色
cell.contentView.backgroundColor= [UIColorclearColor];
cell.imageView.image=[UIImageimageNamed:@"1001.jpg"];//未選cell時(shí)的圖片
cell.imageView.highlightedImage=[UIImageimageNamed:@"1002.jpg"];//選中cell后的圖片
cell.textLabel.text=[[self.myDicobjectForKey:[[self.myDicallKeys]objectAtIndex:indexPath.section]]objectAtIndex:indexPath.row];
returncell;
}
//行縮進(jìn)
-(NSInteger)tableView:(UITableView*)tableView indentationLevelForRowAtIndexPath:(NSIndexPath*)indexPath{
NSUIntegerrow = [indexPathrow];
returnrow;
}
//選中Cell響應(yīng)事件
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
[tableViewdeselectRowAtIndexPath:indexPathanimated:YES];//選中后的反顯顏色即刻消失
//得到當(dāng)前選中的cell
UITableViewCell*cell=[tableViewcellForRowAtIndexPath:indexPath];
NSLog(@"cell=%@",cell);
}
//行將顯示的時(shí)候調(diào)用,預(yù)加載行
-(void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath
{
NSLog(@"將要顯示的行是\n cell=%@\n indexpath=%@",cell,indexPath);
}
//選中之前執(zhí)行,判斷選中的行(阻止選中第一行)
-(NSIndexPath*)tableView:(UITableView*)tableView willSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
NSUIntegerrow = [indexPathrow];
if(row ==0)
returnnil;
returnindexPath;
}
//編輯狀態(tài),點(diǎn)擊刪除時(shí)調(diào)用
- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath*)indexPath
{
}
//cell右邊按鈕格式為UITableViewCellAccessoryDetailDisclosureButton時(shí)形导,點(diǎn)擊按扭時(shí)調(diào)用的方法
-(void)tableView:(UITableView*)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath*)indexPath{
NSLog(@"當(dāng)前點(diǎn)擊的詳情button \n indexpath=%@",indexPath);
}
//右側(cè)添加一個(gè)索引表
- (NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView{
return[self.myDicallKeys];
}
//劃動(dòng)cell是否出現(xiàn)del按鈕
- (BOOL)tableView:(UITableView*)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath {
return YES;
}
//設(shè)定橫向滑動(dòng)時(shí)是否出現(xiàn)刪除按扭,(阻止第一行出現(xiàn))
-(UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath
{
if(indexPath.row==0) {
returnUITableViewCellEditingStyleNone;
}
else{
returnUITableViewCellEditingStyleDelete;
}
returnUITableViewCellEditingStyleDelete;
}
//自定義劃動(dòng)時(shí)delete按鈕內(nèi)容
- (NSString*)tableView:(UITableView*)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexPath{
return@"刪除這行";
}
//開(kāi)始移動(dòng)row時(shí)執(zhí)行
-(void)tableView:(UITableView*)tableView moveRowAtIndexPath:(NSIndexPath*)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath
{
NSLog(@"sourceIndexPath=%@",sourceIndexPath);
NSLog(@"sourceIndexPath=%@",destinationIndexPath);
}
//滑動(dòng)可以編輯時(shí)執(zhí)行
-(void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath*)indexPath
{
NSLog(@"willBeginEditingRowAtIndexPath");
}
//將取消選中時(shí)執(zhí)行,也就是上次先中的行
-(NSIndexPath*)tableView:(UITableView*)tableView willDeselectRowAtIndexPath:(NSIndexPath*)indexPath
{
NSLog(@"上次選中的行是\n indexpath=%@",indexPath);
returnindexPath;
}
//讓行可以移動(dòng)
-(BOOL)tableView:(UITableView*)tableView canMoveRowAtIndexPath:(NSIndexPath*)indexPath
{
return NO;
}
//移動(dòng)row時(shí)執(zhí)行
-(NSIndexPath*)tableView:(UITableView*)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath*)sourceIndexPath toProposedIndexPath:(NSIndexPath*)proposedDestinationIndexPath
{
NSLog(@"targetIndexPathForMoveFromRowAtIndexPath");
//用于限制只在當(dāng)前section下面才可以移動(dòng)
if(sourceIndexPath.section!= proposedDestinationIndexPath.section){
returnsourceIndexPath;
}
returnproposedDestinationIndexPath;
}