Hi I am beginner in objective-c and I am doing expandable list in my project and everything is coming but I want to round corner the Headerview all corner sides along with their expandable cells but according to my code when we expand the cells then cell is coming like First image
but I want to display cell when we expand like second image
please help me some one and this is my code:-
#import "ViewController.h"
@interface ViewController ()
{
UITableView * expandableTableView;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self initialization];
}
-(void)initialization
{
expandableTableView = [[UITableView alloc]init];
expandableTableView.frame = CGRectMake(5,15,310,self.view.frame.size.height - 50);
expandableTableView.dataSource=self;
expandableTableView.delegate=self;
expandableTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[expandableTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
[expandableTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
expandableTableView.backgroundColor = [UIColor whiteColor];
UIEdgeInsets inset = UIEdgeInsetsMake(5, 0, 0, 0);
expandableTableView.contentInset = inset;
[self.view addSubview:expandableTableView];
arrayForBool=[[NSMutableArray alloc]init];
sectionTitleArray=[[NSArray alloc]initWithObjects:
@"India",
@"Usa",
@"Uae",
@"Japan",
@"SouthAfrica",
@"Jarmany",
@"England",
@"Rasya",
nil];
for (int i=0; i<[sectionTitleArray count]; i++) {
[arrayForBool addObject:[NSNumber numberWithBool:NO]];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([[arrayForBool objectAtIndex:section] boolValue]) {
return 1;
}
else
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellid=@"hello";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellid];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
}
BOOL manyCells = [[arrayForBool objectAtIndex:indexPath.section] boolValue];
if(!manyCells)
{
cell.backgroundColor=[UIColor clearColor];
cell.textLabel.text=@"";
}
else
{
cell.textLabel.text=[NSString stringWithFormat:@"%@ %d",[sectionTitleArray objectAtIndex:indexPath.section],indexPath.row+1];
cell.textLabel.font=[UIFont systemFontOfSize:15.0f];
cell.backgroundColor=[UIColor lightGrayColor];
cell.imageView.image=[UIImage imageNamed:@"point.png"];
}
cell.textLabel.textColor=[UIColor blackColor];
[cell.layer setCornerRadius:7.0f];
[cell.layer setMasksToBounds:YES];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [sectionTitleArray count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[arrayForBool replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithBool:NO]];
[expandableTableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[arrayForBool objectAtIndex:indexPath.section] boolValue]) {
return 100;
}
return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *footerView;
footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 10)];
footerView.backgroundColor = [UIColor whiteColor];
return footerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *sectionView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 280,40)];
sectionView.tag=section;
sectionView.backgroundColor = [UIColor lightGrayColor];
UILabel *viewLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, expandableTableView.frame.size.width-10, 40)];
viewLabel.backgroundColor=[UIColor clearColor];
viewLabel.textColor=[UIColor blackColor];
viewLabel.font=[UIFont systemFontOfSize:15];
viewLabel.text=[NSString stringWithFormat:@"List of %@",[sectionTitleArray objectAtIndex:section]];
[sectionView addSubview:viewLabel];
sectionView.layer.cornerRadius = 5.0;
sectionView.clipsToBounds = YES;
UITapGestureRecognizer *headerTapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)];
[sectionView addGestureRecognizer:headerTapped];
return sectionView;
}
- (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:gestureRecognizer.view.tag];
if (indexPath.row == 0) {
BOOL collapsed = [[arrayForBool objectAtIndex:indexPath.section] boolValue];
for (int i=0; i<[sectionTitleArray count]; i++) {
if (indexPath.section==i) {
[arrayForBool replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:!collapsed]];
}
}
[expandableTableView reloadSections:[NSIndexSet indexSetWithIndex:gestureRecognizer.view.tag] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
[cell setPreservesSuperviewLayoutMargins:NO];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end