關(guān)于iOS中的TableView

在使用tableview 前 記得設(shè)置 Delegate 和DataSource

UITableView = _TabView;

-(void)viewDidLoad{

[super viewDidLoad];

_TabView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

_TabView.delegate = self;

_TabView.dataSource = self;

//設(shè)置cell 的 分行樣式 默認(rèn)為UITableViewCellSeparatorStyleSingleLine

[_TabView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];

//設(shè)置cell 行 分割線 的顏色

_TabView.separatorColor = [UIColor redColor];

//設(shè)置行高

_TabView.rowHeight = 120;

//設(shè)置行 Header 的高度

_TabView.sectionHeaderHeight = 100;

//設(shè)置行 Fotter 的高度

_TabView.sectionFooterHeight = 100;

//跳轉(zhuǎn)到指定的 分區(qū) 和 行

[_TabView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1] atScrollPosition:UITableViewScrollPositionTop animated:YES];

[self.view addSubview:_TabView];

}

//設(shè)置表格的分區(qū)數(shù)

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

return 10;

}

//每個(gè)section底部標(biāo)題高度(實(shí)現(xiàn)這個(gè)代理方法后前面 sectionHeaderHeight 設(shè)定的高度無(wú)效)

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

return 20;

}

//每個(gè)section頭部標(biāo)題高度(實(shí)現(xiàn)這個(gè)代理方法后前面 sectionFooterHeight 設(shè)定的高度無(wú)效)

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

return 20;

}

//設(shè)定每個(gè)分區(qū)section 頭部的標(biāo)題

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

return? @"header";

//return [[dictonary allKeys] objectAtIndex:section];字典 賦 給頭部標(biāo)題

}

//設(shè)定每隔分區(qū)的底部標(biāo)題 footer

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

return? @"footer";

}

//用 自定制 自定義的 section頭部視圖-Header

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

return nil;

}

//用以定制自定義的section底部視圖-Footer

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,320, 20)];

imageView.image=[UIImage imageNamed:@"1000.png"];

return imageView;

}

//設(shè)置每個(gè)分區(qū)內(nèi)的行數(shù)

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

if (section == 0) {

return 10;

}else if(section == 1){

return 5;

}

return 0;

}

//創(chuàng)建cell 及 給cell 賦值{

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString * cellid = @"cellid";

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellid];

if (!cell) {

cell? =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];

//設(shè)定選中cell時(shí)的cell的背影顏色

//選中時(shí)藍(lán)色效果

cell.selectionStyle=UITableViewCellSelectionStyleBlue;

//cell.selectionStyle=UITableViewCellSelectionStyleNone; //選中時(shí)沒(méi)有顏色效果

//cell.selectionStyle=UITableViewCellSelectionStyleGray;? //選中時(shí)灰色效果

//自定義選中cell時(shí)的背景顏色

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

//selectedView.backgroundColor = [UIColor orangeColor];

//cell.selectedBackgroundView = selectedView;

//[selectedView release];

//行不能被選中

// cell.selectionStyle=UITableViewCellAccessoryNone;

//這是設(shè)置沒(méi)選中之前的背景顏色

cell.contentView.backgroundColor = [UIColor clearColor];

//未選cell時(shí)的圖片

cell.imageView.image=[UIImage imageNamed:@"1001.jpg"];

//選中cell后的圖片

cell.imageView.highlightedImage=[UIImage imageNamed:@"1002.jpg"];

}

//每一行上添加圖片

//每一行上添加主標(biāo)題

cell.textLabel.text = @"你好";

//每一行上添加子標(biāo)題

cell.detailTextLabel.text = @"敦敦";

//從字典中獲取數(shù)據(jù)

//cell.textLabel.text=[[self.myDic objectForKey:[[self.myDicallKeys]objectAtIndex:indexPath.section]]objectAtIndex:indexPath.row];

//從Model中獲取數(shù)據(jù)

Model * model = arr[indexPath.row];(用到網(wǎng)絡(luò)的時(shí)候要注明跳轉(zhuǎn)時(shí)給下一個(gè)界面賦地址)

//每一行上添加按鈕

return cell;

}

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

return 100;

}

//======================cell的點(diǎn)擊跳轉(zhuǎn)事件

//選中Cell響應(yīng)事件

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

//選中后的反顯顏色即刻消失

[tableView deselectRowAtIndexPath:indexPath animated:YES];

//得到當(dāng)前選中的cell

UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];

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

//點(diǎn)擊不同的cell 不同的響應(yīng)事件

//? ? self.navigationController.navigationBar.hidden = NO;

//? ? if (indexPath.section == 0)

//? ? {

//? ? ? ? if (indexPath.row == 0)

//? ? ? ? {

//? ? ? ? ? ? NSLog(@"點(diǎn)擊了1-1");

//? ? ? ? }

//? ? ? ? else if (indexPath.row == 1)

//? ? ? ? {

//? ? ? ? ? ? NSLog(@"點(diǎn)擊了1-2");

//? ? ? ? }

//? ? ? ? else if (indexPath.row == 2)

//? ? ? ? {

//? ? ? ? ? ? NSLog(@"點(diǎn)擊了1-3");

//? ? ? ? }

//? ? }else if (indexPath.section == 1)

//? ? {

//? ? ? ? if (indexPath.row == 1){

//? ? ? ? ? ? NSLog(@"點(diǎn)擊了2-1");

//? ? ? ? }else if(indexPath.row == 2){

//? ? ? ? ? ? NSLog(@"點(diǎn)擊了2-2");

//? ? ? ? }

//? ? }

}

------------------冊(cè)通知時(shí)候

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notificationUpdate:) name:@"data" object:nil];

調(diào)用業(yè)務(wù)處理類(lèi)獲取數(shù)據(jù)

[[LoadData shareLoadData]getData];

//設(shè)置通知響應(yīng)方法

- (void)notificationUpdate:(NSNotification *)notifi

{

arr = [notifi.object copy];

//刷新表格

[table reloadData];

}

--------------------------------

//=====================cell 左滑時(shí)的事件

//cell的左滑刪除事件

// 設(shè)置 cell 是否允許左滑

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

return true;

}

//// 設(shè)置默認(rèn)的左滑按鈕的title

//-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {

//? ? return @"按鈕鈕鈕";

//}

//// 點(diǎn)擊左滑出現(xiàn)的按鈕時(shí)觸發(fā)

//-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

//? ? NSLog(@"點(diǎn)擊左滑出現(xiàn)的按鈕時(shí)觸發(fā)");

//}

//// 左滑結(jié)束時(shí)調(diào)用(只對(duì)默認(rèn)的左滑按鈕有效,自定義按鈕時(shí)這個(gè)方法無(wú)效)

//-(void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath {

//? ? NSLog(@"左滑結(jié)束");

//}

// 自定義左滑cell時(shí)的按鈕和觸發(fā)方法

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{

// 創(chuàng)建第一個(gè)按鈕和觸發(fā)事件

UITableViewRowAction *cellActionA = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"按鈕-1" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){

// 在此寫(xiě)點(diǎn)擊按鈕時(shí)的觸發(fā)事件

// ......

NSLog(@"點(diǎn)擊了 按鈕-1");

}];

// 定義按鈕的顏色

cellActionA.backgroundColor = [UIColor greenColor];

// 創(chuàng)建第二個(gè)按鈕和觸發(fā)事件

UITableViewRowAction *cellActionB = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"按鈕-2" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){

// 在此寫(xiě)點(diǎn)擊按鈕時(shí)的觸發(fā)事件

// ......

NSLog(@"點(diǎn)擊了 按鈕-2");

}];

// 注意這里返回的是一個(gè)按鈕組,即使只定義了一個(gè)按鈕也要返回一個(gè)組

return @[cellActionA, cellActionB];

}

//行縮進(jìn)

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

//? ? NSUInteger row = [indexPath row];

//? ? return row;

//}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容