How to set up UITableView within a UIViewController created on a .xib file

前端 未结 3 785
一个人的身影
一个人的身影 2021-01-31 21:17

I have a class like this:

@interface ExerciseLogDetails : UIViewController {
         


        
3条回答
  •  不知归路
    2021-01-31 21:46

    Like Eyal said, you shouldn't create a UITableView programmatically and in the Interface Builder. Instead, it is much easier to just create one in Interface Builder and assigns it's delegate and datasource properties to File's Owner in IB.

    Once you've done this, you don't need to create one programmatically and there's no need for a @property for the tableview. Instead, you could have your UIViewController's class files look like this:

    // YourViewController.h
    
    #import 
    
    @interface YourViewController : UIViewController 
    
    @property (strong, nonatomic) NSArray *yourData;
    
    @end
    

    Where the NSArray will contain your data that you will enter into the table programmatically. You may use other data classes too like an NSDictionary depending on what data you have and how you want it to sit in the table.

    // YourViewController.m
    
    #import "YourViewController.h"
    
    @implementation YourViewController
    @synthesize yourData;
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Here you are creating some data to go in your table by inputting it as an array.
        // I just used some basic strings as an example.
        NSArray *array = [[NSArray alloc] initWithObjects:@"Data1", @"Data2", @"Data3", nil];
        // Copying the array you just created to your data array for use in your table.
        self.yourData = array;
    }
    
    - (void)viewDidUnload
    {
        [super viewDidUnload];
        self.yourData = nil;
    }
    
    #pragma mark Table View Data Source Methods
    
    // This will tell your UITableView how many rows you wish to have in each section.
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return [self.yourData count];
    }
    
    // This will tell your UITableView what data to put in which cells in your table.
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifer = @"CellIdentifier";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifer];
    
        // Using a cell identifier will allow your app to reuse cells as they come and go from the screen.
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifer];
        }
    
        // Deciding which data to put into this particular cell.
        // If it the first row, the data input will be "Data1" from the array.
        NSUInteger row = [indexPath row];
        cell.textLabel.text = [yourData objectAtIndex:row];
    
        return cell;
        }
    
    @end
    

    This should just create a simple UITableView with three entries of data that you have entered programmatically.

    If you have any problems or questions just post a comment. :)

    Hope this helps.

提交回复
热议问题