Creating a UITableView Programmatically

后端 未结 9 2107
梦谈多话
梦谈多话 2021-01-31 14:26

I have an application in Xcode 4.6 which uses storyboards. I added a UITableView to a view controller class, which worked as expected. However, when I tried deleting the UITable

9条回答
  •  生来不讨喜
    2021-01-31 15:00

    sample table

     #import "StartreserveViewController.h"
     #import "CollectionViewController.h"
     #import "TableViewCell1.h"
    @interface StartreserveViewController ()
    
    
    {
    NSArray *name;
    NSArray *images;
    NSInteger selectindex;
    
    }
    
    @end
    
    @implementation StartreserveViewController
    
    - (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor blueColor];
    _startReservetable.backgroundColor = [UIColor blueColor];
    
    
    name = [[NSArray alloc]initWithObjects:@"Mobiles",@"Costumes",@"Shoes", 
    nil];
    images = [[NSArray 
     alloc]initWithObjects:@"mobilestitle.jpg",@"costumetitle.jpeg", 
     @"shoestitle.png",nil];
    
    
    
      }
    
     - (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }
    

    pragma mark - UiTableview Datasource

     -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
     {
    return 1;
     }
    
     -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
     (NSInteger)section
     {
    return 3;
     }
    
     - (UITableViewCell *)tableView:(UITableView *)tableView 
      cellForRowAtIndexPath:(NSIndexPath *)indexPath
     {
    
    
    static NSString *cellId = @"tableview";
    
    TableViewCell1  *cell =[tableView dequeueReusableCellWithIdentifier:cellId];
    
    
    cell.cellTxt .text = [name objectAtIndex:indexPath.row];
    
    cell.cellImg.image = [UIImage imageNamed:[images 
    objectAtIndex:indexPath.row]];
    
    return cell;
    
    
    
     }
    
    
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
    (NSIndexPath *)indexPath
     {
    selectindex = indexPath.row;
    [self performSegueWithIdentifier:@"second" sender:self];
    }
    
    
    
       #pragma mark - Navigation
    
     // In a storyboard-based application, you will often want to do a little     
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    if ([segue.identifier isEqualToString:@"second"])
    
      {
        CollectionViewController *obj = segue.destinationViewController;
               obj.receivename = [name objectAtIndex:selectindex];
    
      }
    
    
    
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    }
    
     @end
    

    .h

    #import 
    
    @interface StartreserveViewController :      
    UIViewController    
    @property (strong, nonatomic) IBOutlet UITableView *startReservetable;
    
     @end
    

提交回复
热议问题