HOW TO POPULATE UITABLEVIEWBY PROGRAMMATICALLY?

Pravesh Singh

Total Post:101

Points:709
Posted by  Pravesh Singh
iPhone  Ios  Ios 7 
 1072  View(s)
Ratings:
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

  1. Ankita Pandey

    Post:183

    Points:1285
    Re: How to populate uitableviewby programmatically?

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

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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!