简体   繁体   中英

How to add UITableView in a customize view declared dynamically in iOS?

In my app,in navigation bar one button (say browsebutton ) is there , as i click the button one view (say browseView ) appears which I took dynamically with the help of CGRectMake function.

I added UITableView (say browseTableView ) in the browseView.

Table gets added to the browseView but the delegate methods are not working.

Please give me proper direction.

My code is as follows:

MainViewController.h

@interface MainViewController : UIMainViewController <UITableViewDelegate,UITableViewDataSource>  

MainViewController.m

browseTableView.delegate = self;
browseTableView.dataSource =self;

-(void)BrowseButton {

    browseView = [[UIView alloc]initWithFrame:CGRectMake(0.0, 0.0, 300.0, 500.0)];
    browseView.backgroundColor = [UIColor whiteColor];
    browseView.opaque = NO;
    [browseView.layer setBorderColor: [[UIColor blackColor] CGColor]];
    [browseView.layer setBorderWidth: 4.0];
    [browseView setBackgroundColor:[UIColor clearColor]];
    browseTableView = [[UITableView alloc]initWithFrame:CGRectMake(0.0, 0.0, 300, 460)];
    [browseView addSubview:browseTableView];
    [self.view addSubview:browseView];      
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    categoryArray =  [[NSMutableArrayalloc]initWithObjects:@"Platinum",@"Diamond",
    @"Gold",@"Silver", nil];
    return [categoryArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    cell.textLabel.text = [categoryArray objectAtIndex:indexPath.row];
    return cell;
}  

You forgot to mention:

browseTableView = [[UITableView alloc]initWithFrame:CGRectMake(0.0, 0.0, 300, 460)];
browseTableView.delegate = self;
browseTableView.dataSource = self;
[browseView addSubview:browseTableView];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM