UITableView

NSArray*array1_=@[@"張鐵林",@"張國立",@"張國榮",@"張藝謀",@"張惠妹"];

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;

//改變換行線顏色

myTableView_.separatorColor= [UIColorblueColor];

//設定Header的高度,

myTableView_.sectionHeaderHeight=50;

//設定footer的高度,

myTableView_.sectionFooterHeight=100;

//設定行高

myTableView_.rowHeight=100;

//設定cell分行線的樣式,默認為UITableViewCellSeparatorStyleSingleLine

[myTableView_setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];

//設定cell分行線顏色

[myTableView_setSeparatorColor:[UIColorredColor]];

//編輯tableView

myTableView_.editing=NO;

[self.viewaddSubview:myTableView_];

//跳到指的row or section

[myTableView_scrollToRowAtIndexPath:[NSIndexPathindexPathForRow:2inSection:2]

atScrollPosition:UITableViewScrollPositionBottom animated:NO];

}

//指定有多少個分區(qū)(Section),默認為1

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {

return[[self.myDicallKeys]count];

}

//每個section底部標題高度(實現(xiàn)這個代理方法后前面sectionHeaderHeight設定的高度無效)

-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section{

return20;

}

//每個section頭部標題高度(實現(xiàn)這個代理方法后前面sectionFooterHeight設定的高度無效)

-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section{

return20;

}

//每個section頭部的標題-Header

- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{

return[[self.myDicallKeys]objectAtIndex:section];

}

//每個section底部的標題-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];

}

//指定每個分區(qū)中有多少行,默認為1

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

return[[self.myDicobjectForKey:[[self.myDicallKeys]objectAtIndex:section]]count];

}

//改變行的高度(實現(xiàn)主個代理方法后rowHeight設定的高度無效)

- (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];

//設定附加視圖

[cellsetAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];

//UITableViewCellAccessoryNone,//沒有標示

//UITableViewCellAccessoryDisclosureIndicator,//下一層標示

//UITableViewCellAccessoryDetailDisclosureButton, //詳情button

//UITableViewCellAccessoryCheckmark//勾選標記

//設定選中cell時的cell的背影顏色

cell.selectionStyle=UITableViewCellSelectionStyleBlue;//選中時藍色效果

//cell.selectionStyle=UITableViewCellSelectionStyleNone; //選中時沒有顏色效果

//cell.selectionStyle=UITableViewCellSelectionStyleGray;//選中時灰色效果

//

////自定義選中cell時的背景顏色

//UIView *selectedView = [[UIView alloc] initWithFrame:cell.contentView.frame];

//selectedView.backgroundColor = [UIColor orangeColor];

//cell.selectedBackgroundView = selectedView;

//[selectedView release];

//cell.selectionStyle=UITableViewCellAccessoryNone; //行不能被選中

}

//這是設置沒選中之前的背景顏色

cell.contentView.backgroundColor= [UIColorclearColor];

cell.imageView.image=[UIImageimageNamed:@"1001.jpg"];//未選cell時的圖片

cell.imageView.highlightedImage=[UIImageimageNamed:@"1002.jpg"];//選中cell后的圖片

cell.textLabel.text=[[self.myDicobjectForKey:[[self.myDicallKeys]objectAtIndex:indexPath.section]]objectAtIndex:indexPath.row];

returncell;

}

//行縮進

-(NSInteger)tableView:(UITableView*)tableView indentationLevelForRowAtIndexPath:(NSIndexPath*)indexPath{

NSUIntegerrow = [indexPathrow];

returnrow;

}

//選中Cell響應事件

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{

[tableViewdeselectRowAtIndexPath:indexPathanimated:YES];//選中后的反顯顏色即刻消失

//得到當前選中的cell

UITableViewCell*cell=[tableViewcellForRowAtIndexPath:indexPath];

NSLog(@"cell=%@",cell);

}

//行將顯示的時候調(diào)用,預加載行

-(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ào)用

- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle

forRowAtIndexPath:(NSIndexPath*)indexPath

{

}

//cell右邊按鈕格式為UITableViewCellAccessoryDetailDisclosureButton時,點擊按扭時調(diào)用的方法

-(void)tableView:(UITableView*)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath*)indexPath{

NSLog(@"當前點擊的詳情button \n indexpath=%@",indexPath);

}

//右側添加一個索引表

- (NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView{

return[self.myDicallKeys];

}

//劃動cell是否出現(xiàn)del按鈕

- (BOOL)tableView:(UITableView*)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath {

return YES;

}

//設定橫向滑動時是否出現(xiàn)刪除按扭,(阻止第一行出現(xiàn))

-(UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath

{

if(indexPath.row==0) {

returnUITableViewCellEditingStyleNone;

}

else{

returnUITableViewCellEditingStyleDelete;

}

returnUITableViewCellEditingStyleDelete;

}

//自定義劃動時delete按鈕內(nèi)容

- (NSString*)tableView:(UITableView*)tableView

titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexPath{

return@"刪除這行";

}

//開始移動row時執(zhí)行

-(void)tableView:(UITableView*)tableView moveRowAtIndexPath:(NSIndexPath*)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath

{

NSLog(@"sourceIndexPath=%@",sourceIndexPath);

NSLog(@"sourceIndexPath=%@",destinationIndexPath);

}

//滑動可以編輯時執(zhí)行

-(void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath*)indexPath

{

NSLog(@"willBeginEditingRowAtIndexPath");

}

//將取消選中時執(zhí)行,也就是上次先中的行

-(NSIndexPath*)tableView:(UITableView*)tableView willDeselectRowAtIndexPath:(NSIndexPath*)indexPath

{

NSLog(@"上次選中的行是\n indexpath=%@",indexPath);

returnindexPath;

}

//讓行可以移動

-(BOOL)tableView:(UITableView*)tableView canMoveRowAtIndexPath:(NSIndexPath*)indexPath

{

return NO;

}

//移動row時執(zhí)行

-(NSIndexPath*)tableView:(UITableView*)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath*)sourceIndexPath toProposedIndexPath:(NSIndexPath*)proposedDestinationIndexPath

{

NSLog(@"targetIndexPathForMoveFromRowAtIndexPath");

//用于限制只在當前section下面才可以移動

if(sourceIndexPath.section!= proposedDestinationIndexPath.section){

returnsourceIndexPath;

}

returnproposedDestinationIndexPath;

}

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容