UITableView

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;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市习霹,隨后出現(xiàn)的幾起案子朵耕,更是在濱河造成了極大的恐慌,老刑警劉巖淋叶,帶你破解...
    沈念sama閱讀 216,372評(píng)論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件撑蚌,死亡現(xiàn)場(chǎng)離奇詭異书聚,居然都是意外死亡歼捏,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén)栅贴,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人熏迹,你說(shuō)我怎么就攤上這事筹误。” “怎么了癣缅?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,415評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵厨剪,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我友存,道長(zhǎng)祷膳,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,157評(píng)論 1 292
  • 正文 為了忘掉前任屡立,我火速辦了婚禮直晨,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘膨俐。我一直安慰自己勇皇,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布焚刺。 她就那樣靜靜地躺著敛摘,像睡著了一般。 火紅的嫁衣襯著肌膚如雪乳愉。 梳的紋絲不亂的頭發(fā)上兄淫,一...
    開(kāi)封第一講書(shū)人閱讀 51,125評(píng)論 1 297
  • 那天,我揣著相機(jī)與錄音蔓姚,去河邊找鬼捕虽。 笑死,一個(gè)胖子當(dāng)著我的面吹牛坡脐,可吹牛的內(nèi)容都是我干的泄私。 我是一名探鬼主播,決...
    沈念sama閱讀 40,028評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼备闲,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼晌端!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起浅役,我...
    開(kāi)封第一講書(shū)人閱讀 38,887評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤斩松,失蹤者是張志新(化名)和其女友劉穎伶唯,沒(méi)想到半個(gè)月后觉既,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,310評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評(píng)論 2 332
  • 正文 我和宋清朗相戀三年瞪讼,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了钧椰。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,690評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡符欠,死狀恐怖嫡霞,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情希柿,我是刑警寧澤诊沪,帶...
    沈念sama閱讀 35,411評(píng)論 5 343
  • 正文 年R本政府宣布,位于F島的核電站曾撤,受9級(jí)特大地震影響端姚,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜挤悉,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評(píng)論 3 325
  • 文/蒙蒙 一渐裸、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧装悲,春花似錦昏鹃、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至属瓣,卻和暖如春您宪,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背奠涌。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,812評(píng)論 1 268
  • 我被黑心中介騙來(lái)泰國(guó)打工宪巨, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人溜畅。 一個(gè)月前我還...
    沈念sama閱讀 47,693評(píng)論 2 368
  • 正文 我出身青樓捏卓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親慈格。 傳聞我的和親對(duì)象是個(gè)殘疾皇子怠晴,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評(píng)論 2 353

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