Home > DeveloperSection > Forums > How to populate uitableviewby programmatically?
Pravesh Singh
Pravesh Singh

Total Post:101

Points:709
Posted on    October-17-2014 11:37 PM

 iPhone iPhone  Ios  Ios 7 
Ratings:


 1 Reply(s)
 623  View(s)
Rate this:

I am creating a custom drop down menu.On button click i am unhiding the UIView which is subview of my root view. Inside that subview i have added UItableview programmatically.Till this my code is working fine,but i am stuck while populating the table with my data array.so can anybody help me to solve this problem..... thanks in advance

I have assigned the UItableView delegate and datasource also..

Here is my implementation

- (void)viewDidLoad

{

[super viewDidLoad];

self.grades = [[NSArray alloc]initWithObjects:@"Excellent",@"Good",@"ok",@"Not Good", nil];

self.tableView.delegate = self;

self.tableView.dataSource = self;

self.coverView = [[UIView alloc]init];

self.shadowView = [[UIView alloc]init];

self.coverView = [[UIView alloc] initWithFrame:CGRectMake(90.0f, 220.0f, 150.0f, 200.0f)];

self.coverView.backgroundColor = [UIColor whiteColor];

CALayer * layer = self.coverView.layer;

layer.shadowOffset = CGSizeMake(1, 1);

layer.shadowColor = [[UIColor blackColor] CGColor];

layer.shadowRadius = 4.0f;

layer.shadowOpacity = 0.80f;

layer.shadowPath = [[UIBezierPath bezierPathWithRect:layer.bounds] CGPath];

[[self view] addSubview:self.coverView];

self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 140.0f, 200.0f)];

[self.coverView addSubview:self.tableView];

self.coverView.hidden = YES;

[self.tableView reloadData];

}

- (IBAction)buttonClicked:(id)sender

{

CGRect screenRect = [[UIScreen mainScreen] bounds];

self.shadowView = [[UIView alloc] initWithFrame:screenRect];

self.shadowView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];

[self.view addSubview:self.shadowView];

[[self view] addSubview:self.coverView];

self.coverView.hidden = NO;

[self fadeIn];

[self.tableView reloadData];

}

- (IBAction)screenTapped:(id)sender

{

NSLog(@"Screen Tapped");

[self fadeOut];

self.shadowView.hidden = YES;

[self.shadowView removeFromSuperview];

}

- (void)fadeIn

{

self.coverView.transform = CGAffineTransformMakeScale(0.5, 0.5);

self.coverView.alpha = 0;

[UIView animateWithDuration:.35 animations:^{

    self.coverView.alpha = 1;

    self.coverView.transform = CGAffineTransformMakeScale(1.4,1.4);

}];

}

- (void)fadeOut

{

[UIView animateWithDuration:.35 animations:^{

    self.coverView.transform = CGAffineTransformMakeScale(1.3, 1.3);

    self.coverView.alpha = 0.0;

} completion:^(BOOL finished){

    if (finished)

    {

        [self.coverView removeFromSuperview];

    }

}];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return [self.grades count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

NSString * cellIdentifier = @"Cell";

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil)

{

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

}

cell.textLabel.text = [self.grades objectAtIndex:indexPath.row];

return cell;

}

ios objective-c iphone uitableview



Ankita Pandey
Ankita Pandey

Total Post:183

Points:1285
Posted on    October-17-2014 11:38 PM

looks like you need to set delegates after [UITableView alloc].

self.tableView = [[UITableView alloc]
self.tableView.delegate = self;
self.tableView.dataSource = self;

Don't want to miss updates? Please click the below button!

Follow MindStick