//UITableView
// 去掉多餘的表格 tableView.tableFooterView = [[UIView alloc]init];code
// 去掉cell之間的線 tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
// 設置cell右邊的設置 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; // UITableViewCellAccessoryDisclosureIndicator, // UITableViewCellAccessoryDetailDisclosureButton, // UITableViewCellAccessoryCheckmark, // UITableViewCellAccessoryDetailButtonci
======================================================—-get
// if (_open[section] == 1) { // return 0; // }it
Province * province = _provinces[section]; if (province.isOpen) { return province.cityCount; } return 0;
}io
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{table
static NSString * cellID = @"cellID";cli
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];select
if (!cell) { cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID]autorelease]; }sso
// 刷新cell Province * province = _provinces[indexPath.section]; NSArray * cities = province.cities; cell.textLabel.text = cities[indexPath.row];queue
return cell;
}
// --UITableViewDelegate
(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 60; }
(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ static NSString * headerID = @"headerID";
UITableViewHeaderFooterView * headerView = [tableView dequeueReusableCellWithIdentifier:headerID];
if (!headerView) { headerView = [[[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:headerID]autorelease];
// 添加label UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 60)]; label.tag = 1000; label.backgroundColor = [UIColor lightGrayColor]; [headerView.contentView addSubview:label]; label.userInteractionEnabled = YES; UITapGestureRecognizer * tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clicked:)]; [headerView.contentView addGestureRecognizer:tap]; [tap release]; UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.bounds.size.width/2-16, 25-16, 32, 32)]; imageView.tag = 501; [headerView.contentView addSubview:imageView]; [imageView release]; [label release];
}
headerView.contentView.tag = section + 100;
// 刷新頁頭 UILabel * label = (UILabel *)[headerView.contentView viewWithTag:1000]; Province * province = _provinces[section];
label.text = province.proName; UIImageView * imageView = (UIImageView *)[headerView.contentView viewWithTag:501];
// NSString * imageName = _open[section]?@"arrow_fold.png":@"arrow_spread"; // imageView.image = [UIImage imageNamed:imageName]; if (province.isOpen) { imageView.image = [UIImage imageNamed:@"arrow_spread"]; }else{ imageView.image = [UIImage imageNamed:@"arrow_fold"]; }
return headerView;
}
#pragma mark --手勢觸發的方法
(void)clicked:(UITapGestureRecognizer *)tap{
UIView * view = tap.view; NSLog(@"%ld",view.tag);
// 當前的分組 NSInteger section = view.tag - 100;
// 異或,兩個值都爲0,結果爲0,一個爲1,一個爲0 結果爲1 //
// _open[section] ^= 1; Province * province = _provinces[section];
// if (province.isOpen) { // province.isOpen = NO; // }else{ // province.isOpen = YES; // }
province.isOpen = !province.isOpen; [_tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationBottom];
}
@end