// AppDelegate.m
#import "ViewController.h"
ViewController * theVc = [[ViewController alloc]init];
UINavigationController * theNgc = [[UINavigationController alloc]initWithRootViewController:theVc];
self.window.rootViewController = theNgc;
viewController.m
<UITableViewDelegate,UITableViewDataSource>
{
UITableView * theTable;
NSMutableArray * theArr;
NSMutableArray * arr1,*arr2,*arr3;
NSMutableArray * allArr;
NSMutableArray * arrForBtn;
BOOL flag1,flag2,flag3;
UIButton * theBtn1,*theBtn2,*theBtn3;
}
//========================
theTable = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
theTable.delegate = self;
theTable.dataSource = self;
[self.view addSubview:theTable];
theArr = [NSMutableArray array];
arr1 = [NSMutableArray arrayWithObjects:@"米老鼠",@"唐老鸭",@"汤姆", nil];
arr2 = [NSMutableArray arrayWithObjects:@"么么",@"影响",@"黄飞鸿", nil];
arr3 = [NSMutableArray arrayWithObjects:@"DS",@"DD",@"HH", nil];
allArr = [NSMutableArray arrayWithObjects:arr1,arr2,arr3, nil];
flag1 = 0;
flag2 = 0;
flag3 = 0;
arrForBtn = [NSMutableArray array];
//=======================
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section{
return 50;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIButton * theBtn;
UIView * theVc;
switch (section) {
case 0:
{
theVc = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
theBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
theBtn.frame = CGRectMake(10, 3, 70, 37);
[theBtn setTitle:@"我的好友" forState:0];
theBtn.tag = 0;
}
break;
case 1:
{
theVc = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
theBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
theBtn.frame = CGRectMake(10, 3, 70, 37);
[theBtn setTitle:@"同学" forState:0];
theBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
theBtn.tag = 1;
}
break;
case 2:
{
theVc = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
theBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
theBtn.frame = CGRectMake(10, 3, 100, 37);
[theBtn setTitle:@"我的童年" forState:0];
theBtn.tag = 2;
}
break;
default:
break;
}
[theBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[theVc addSubview:theBtn];
return theVc;
}
-(void)click:(UIButton *)sender{
if (arrForBtn.count > 0) {
BOOL ff = 0;
for (int i = 0; i < arrForBtn.count; i++) {
UIButton * theBtn = arrForBtn[i];
if (theBtn.tag == sender.tag) {
[arrForBtn removeObjectAtIndex:i];
ff = 1;
}
}
if (!ff) {
[arrForBtn addObject:sender];
}
}else{
[arrForBtn addObject:sender];
}
[theTable reloadData];
}
//分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (arrForBtn.count > 0) {
for (UIButton *btn in arrForBtn) {
if (btn.tag == section) {
return [allArr[section] count];
}
}
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell * theCell = [tableView dequeueReusableCellWithIdentifier:@""];
if (!theCell) {
theCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@""];
}
switch (indexPath.section) {
case 0:
{
theCell.textLabel.text = arr1[indexPath.row];
}
break;
case 1:
{
theCell.textLabel.text = arr2[indexPath.row];
}
break;
case 2:
{
theCell.textLabel.text = arr3[indexPath.row];
}
break;
default:
break;
}
return theCell;
}
网友评论