Creating a UITableView Programmatically

后端 未结 9 2297
梦谈多话
梦谈多话 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:02

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
    
        tableView.backgroundColor = [UIColor grayColor];
    
        // add to superview
        [self.view addSubview:tableView];
    }
    
    #pragma mark - UITableViewDataSource
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView
    {
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:    (NSInteger)section
    {
        return 1;
    }
    
    // the cell will be returned to the tableView
    - (UITableViewCell *)tableView:(UITableView *)theTableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *cellIdentifier = @"HistoryCell";
    
        // Similar to UITableViewCell, but 
        UITableViewCell *cell = (UITableViewCell *)[theTableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        cell.descriptionLabel.text = @"Testing";
        return cell;
    }
    

提交回复
热议问题