UITableView使用詳解
在開發iphone的應用時基本上都要用到UITableView,這裡講解一下UITableView的使用方法及代理的呼叫情況
- (void)viewDidLoad
{
[superviewDidLoad];
//初始化資料
NSArray *array1_=@[@"張鐵林",@"張國立",@"張國榮",@"張藝謀",@"張惠妹"];
NSArray*array2_=@[@"李小龍",@"李小路"];
NSArray*array3_=@[@"王剛"];
self.myDic=@{@"老張家":array1_,@"老李家":array2_,@"老王家":array3_};
UITableView *myTableView_=[[UITableView alloc] initWithFrame:CGRectMake(0,0, 320,460) style:UITableViewStylePlain];
myTableView_.delegate=self;
myTableView_.dataSource=self;
//改變換行線顏色lyttzx.com
myTableView_.separatorColor = [UIColor blueColor];
//設定Header的高度,
myTableView_.sectionHeaderHeight=50;
//設定footer的高度,
myTableView_.sectionFooterHeight=100;
//設定行高
myTableView_.rowHeight=100;
//設定cell分行線的樣式,預設為UITableViewCellSeparatorStyleSingleLine
[myTableView_ setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
//設定cell分行線顏色
[myTableView_ setSeparatorColor:[UIColor redColor]];
//編輯tableView
myTableView_.editing=NO;
[self.view addSubview:myTableView_];
//跳到指的rowor section
[myTableView_ scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:2inSection:2]
atScrollPosition:UITableViewScrollPositionBottomanimated:NO];
}
//指定有多少個分割槽(Section),預設為1
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {
return[[self.myDic allKeys] count];
}
//每個section底部標題高度(實現這個代理方法後前面sectionHeaderHeight 設定的高度無效)
-(CGFloat)tableView:(UITableView *)tableViewheightForHeaderInSection:(NSInteger)section{
return20;
}
//每個section頭部標題高度(實現這個代理方法後前面sectionFooterHeight 設定的高度無效)
-(CGFloat)tableView:(UITableView *)tableViewheightForFooterInSection:(NSInteger)section{
return20;
}
//每個section頭部的標題-Header
- (NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section{
return[[self.myDic allKeys] objectAtIndex:section];
}
//每個section底部的標題-Footer
- (NSString *)tableView:(UITableView *)tableViewtitleForFooterInSection:(NSInteger)section{
return nil;
}
//用以定製自定義的section頭部檢視-Header
-(UIView *)tableView:(UITableView *)tableViewviewForHeaderInSection:(NSInteger)section{
return nil;
}
//用以定製自定義的section底部檢視-Footer
-(UIView *)tableView:(UITableView *)tableViewviewForFooterInSection:(NSInteger)section{
UIImageView*imageView_=[[UIImageViewalloc]initWithFrame:CGRectMake(0, 0,320, 20)];
imageView_.image=[UIImage imageNamed:@"1000.png"];
return[imageView_ autorelease];
}
//指定每個分割槽中有多少行,預設為1
- (NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section{
return [[self.myDicobjectForKey:[[self.myDicallKeys]objectAtIndex:section]] count];
}
//改變行的高度(實現主個代理方法後rowHeight設定的高度無效)
- (CGFloat)tableView:(UITableView *)tableViewheightForRowAtIndexPath:(NSIndexPath *)indexPath{
return100;
}
//繪製Cell
-(UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath {
staticNSString *SimpleTableIdentifier= @"SimpleTableIdentifier";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
if (cell ==nil) {
cell = [[[UITableViewCell alloc] 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;
// [selectedViewrelease];
// cell.selectionStyle=UITableViewCellAccessoryNone; //行不能被選中
}
//這是設定沒選中之前的背景顏色
cell.contentView.backgroundColor = [UIColor clearColor];
cell.imageView.image=[UIImage imageNamed:@"1001.jpg"];//未選cell時的圖片
cell.imageView.highlightedImage=[UIImage imageNamed:@"1002.jpg"];//選中cell後的圖片
cell.textLabel.text=[[self.myDic objectForKey:[[self.myDic allKeys]objectAtIndex:indexPath.section]]objectAtIndex:indexPath.row];
returncell;
}
//行縮排
-(NSInteger)tableView:(UITableView *)tableViewindentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
NSUIntegerrow = [indexPath row];
returnrow;
}
//選中Cell響應事件
- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];//選中後的反顯顏色即刻消失
//得到當前選中的cell
UITableViewCell *cell=[tableViewcellForRowAtIndexPath:indexPath];
NSLog(@"cell=%@",cell);
}
//行將顯示的時候呼叫,預載入行
-(void)tableView:(UITableView *)tableViewwillDisplayCell:(UITableViewCell *)cellforRowAtIndexPath:(NSIndexPath*)indexPath
{
NSLog(@"將要顯示的行是\ncell=%@ \n indexpath=%@",cell,indexPath);
}
//選中之前執行,判斷選中的行(阻止選中第一行)
-(NSIndexPath *)tableView:(UITableView *)tableViewwillSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUIntegerrow = [indexPath row];
if (row ==0)
return nil;
returnindexPath;
}
//編輯狀態,點選刪除時呼叫
- (void)tableView:(UITableView *)tableViewcommitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
}
//cell右邊按鈕格式為UITableViewCellAccessoryDetailDisclosureButton時,點選按扭時呼叫的方法
-(void)tableView:(UITableView *)tableViewaccessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
NSLog(@"當前點選的詳情button\n indexpath=%@",indexPath);
}
//右側新增一個索引表
- (NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView{
return[self.myDic allKeys];
}
//划動cell是否出現del按鈕
- (BOOL)tableView:(UITableView *)tableViewcanEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
//設定橫向滑動時是否出現刪除按扭,(阻止第一行出現)
-(UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row==0) {
returnUITableViewCellEditingStyleNone;
}
else{
returnUITableViewCellEditingStyleDelete;
}
returnUITableViewCellEditingStyleDelete;
}
//自定義划動時delete按鈕內容
- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexPath{
return @"刪除這行";
}
//開始移動row時執行
-(void)tableView:(UITableView *)tableViewmoveRowAtIndexPath:(NSIndexPath*)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath
{
NSLog(@"sourceIndexPath=%@",sourceIndexPath);
NSLog(@"sourceIndexPath=%@",destinationIndexPath);
}
//滑動可以編輯時執行
-(void)tableView:(UITableView *)tableViewwillBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"willBeginEditingRowAtIndexPath");
}
//將取消選中時執行,也就是上次先中的行
-(NSIndexPath *)tableView:(UITableView *)tableViewwillDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"上次選中的行是 \n indexpath=%@",indexPath);
returnindexPath;
}
//讓行可以移動
-(BOOL)tableView:(UITableView *)tableViewcanMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
//移動row時執行
-(NSIndexPath *)tableView:(UITableView *)tableViewtargetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPathtoProposedIndexPath:(NSIndexPath*)proposedDestinationIndexPath
{
NSLog(@"targetIndexPathForMoveFromRowAtIndexPath");
//用於限制只在當前section下面才可以移動
if(sourceIndexPath.section !=proposedDestinationIndexPath.section){
returnsourceIndexPath;
}
returnproposedDestinationIndexPath;
}
轉至:http://blog.sina.com.cn/s/blog_9693f61a01016lv5.html
相關文章
- 優雅的使用UITableViewUIView
- 詳細整理iOS中UITableView的效能最佳化iOSUIView
- 如何優雅的對UITableView進行解耦UIView解耦
- mydumper使用詳解
- babel使用詳解Babel
- Thymeleaf使用詳解
- NSTimer使用詳解
- SourceInsight使用詳解
- git使用詳解Git
- Jpa使用詳解
- Supervisor使用詳解
- Proxy使用詳解
- LOMBOK使用詳解Lombok
- FastJson使用詳解ASTJSON
- nvm 使用詳解
- mitmproxy使用詳解MIT
- Mat使用詳解
- Logstash使用詳解
- iOS使用UITableView實現的富文字編輯器iOSUIView
- 一行程式碼解決UITableView鍵盤收起行程UIView
- Android AIDL使用詳解AndroidAI
- LiveData && ViewModel使用詳解LiveDataView
- React Hooks 使用詳解ReactHook
- CMAKE的使用詳解
- Go Modules 詳解使用Go
- MFC——SkinMagic使用詳解
- JWT 完整使用詳解JWT
- Postman 使用教程詳解Postman
- APScheduler的使用詳解
- sed指令使用詳解
- Android BroadcastReceiver使用詳解AndroidAST
- 【譯】LiveData 使用詳解LiveData
- Android Gson使用詳解Android
- oracle oradebug使用詳解Oracle
- iOS UITableView 修改屬性iOSUIView
- iOS全埋點解決方案-UITableView和UICollectionView點選事件iOSUIView事件
- Flutter之DataTable使用詳解Flutter
- Python中字典使用詳解Python
- chrome devtools使用詳解——PerformanceChromedevORM