how to write TableViewController in objective-c

Generated on 8/2/2024

1 search

To write a UITableViewController in Objective-C, you can follow these steps:

  1. Create a new class that inherits from UITableViewController.

  2. Implement the required data source methods to populate the table view.

  3. Optionally, implement delegate methods to handle user interactions.

Here is a basic example:

#import <UIKit/UIKit.h>

@interface MyTableViewController : UITableViewController

@property (nonatomic, strong) NSArray *dataArray;

@end

@implementation MyTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Initialize your data array
    self.dataArray = @[@"Item 1", @"Item 2", @"Item 3"];
    
    // Register a cell class for reuse
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
    // Configure the cell
    cell.textLabel.text = self.dataArray[indexPath.row];
    
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Handle row selection
    NSLog(@"Selected row: %@", self.dataArray[indexPath.row]);
}

@end

Explanation:

  1. viewDidLoad: This method is called after the view controller has loaded its view hierarchy into memory. Here, you initialize your data array and register a cell class for reuse.

  2. numberOfSectionsInTableView:: This method returns the number of sections in the table view. In this example, there is only one section.

  3. tableView:numberOfRowsInSection:: This method returns the number of rows in a given section. It uses the count of the dataArray.

  4. tableView:cellForRowAtIndexPath:: This method configures and returns a cell for a given index path. It dequeues a reusable cell and sets its text label to the corresponding item in the dataArray.

  5. tableView:didSelectRowAtIndexPath:: This delegate method is called when a row is selected. Here, it simply logs the selected item.

For more advanced features and customization, you can refer to the What's new in UIKit session from WWDC 2024, which covers updates and new APIs in UIKit, including improvements to table views.

Migrate your TVML app to SwiftUI

Migrate your TVML app to SwiftUI

SwiftUI helps you build great apps on all Apple platforms and is the preferred toolkit for bringing your content into the living room with tvOS 18. Learn how to use SwiftUI to create familiar layouts and controls from TVMLKit, and get tips and best practices.

Elevate your tab and sidebar experience in iPadOS

Elevate your tab and sidebar experience in iPadOS

iPadOS 18 introduces a new navigation system that gives people the flexibility to choose between using a tab bar or sidebar. The newly redesigned tab bar provides more space for content and other functionality. Learn how to use SwiftUI and UIKit to enable customization features – like adding, removing and reordering tabs – to enable a more personal touch in your app.

What’s new in UIKit

What’s new in UIKit

Explore everything new in UIKit, including tab and document launch experiences, transitions, and text and input changes. We’ll also discuss better-than-ever interoperability between UIKit and SwiftUI animations and gestures, as well as general improvements throughout UIKit.

Demystify explicitly built modules

Demystify explicitly built modules

Explore how builds are changing in Xcode 16 with explicitly built modules. Discover how modules are used to build your code, how explicitly built modules improve transparency in compilation tasks, and how you can optimize your build by sharing modules across targets.

What’s new in SwiftUI

What’s new in SwiftUI

Learn how you can use SwiftUI to build great apps for any Apple platform. Explore a fresh new look and feel for tabs and documents on iPadOS. Improve your window management with new windowing APIs, and gain more control over immersive spaces and volumes in your visionOS apps. We’ll also take you through other exciting refinements that help you make expressive charts, customize and layout text, and so much more.