UITableViewspa
- (void)viewDidLoad {代理
[super viewDidLoad];orm
self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 568) style:UITableViewStyleGrouped];分組形式的索引
self.myTableView.delegate = self;事件
self.myTableView.dataSource = self; <UITableViewDelegate,UITableViewDataSource>兩個代理都要遵照string
self.myTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; 分割線it
[self.myTableView setEditing:YES]; 編輯狀態io
[self.view addSubview:_myTableView];table
}class
1.有多少段 默認爲0
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 26;
}
2.有多少行,調用屢次
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if ((section+1)%2 == 0) {
return 3;
}else{
return 2;
}
}
3.某段某行顯示什麼樣子 NSIndexPath封裝的兩個屬性section和row
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//重複利用機制
NSString *cellID = @"cellID";
//先判斷是否有能夠重複利用的cell
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell==nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
//cell顯示的樣式
cell.textLabel.text = [NSString stringWithFormat:@"%ld,%ld",indexPath.section,indexPath.row];
return cell;
}
4.配置cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 80;
}
5.段落頭部尾部
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return @"頭標題";
}
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
return @"尾標題";
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 30;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 30;//不能沒有,若是想沒有就把值變小
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView * v =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
v.backgroundColor = [UIColor redColor];
return v;
}
6.cell被點了要作什麼
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];取消點擊事件
}
7.設置cell是否能夠編輯
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row== 0) {
return NO;
}else{
return YES;
}
}
8.更改編輯狀態的按鈕
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete;
}
9.更改刪除狀態下右邊顯示的標題
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"刪除";
}
10.刪除狀態下按鈕被點
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
}
11.cell能夠上下移動
-(bool)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
}
12.索引標題
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
NSMutableArray * titleArray = [NSMutableArray array];
for (int i = 0; i<26; i++) {
[titleArray addObject:[NSString stringWithFormat:@"%c",'A'+i]];
}
return titleArray;
}