Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use TableView inside viewcontroller?

In the storyboard I have added a table view to my view controller, I have ctrl dragged the TableView to to the Viewcontroller and connected "delegate" and "datasource". In the (.h) file I have added <UITableViewDataSource,UITableViewDelegate> but when I run the app I just get a SIGABRT error (?) and the app crashes. What should I do?

like image 997
b3rge Avatar asked Jul 08 '13 19:07

b3rge


People also ask

Can we use TableView in SwiftUI?

Note: Lists also have the built-in scroll feature same as UITableView in Swift. A static list looks easy and simple to create. Similarly, the dynamic list also uses the same syntax with some additions to that.

How does a TableView work?

A table view displays a single column of vertically scrolling content, divided into rows and sections. Each row of a table displays a single piece of information related to your app. Sections let you group related rows together. For example, the Contacts app uses a table to display the names of the user's contacts.


1 Answers

So far so good, you just have to implement UITableViewDataSource and UITableViewDelegate in your implementation file.

Required functions are as follows;

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [regions count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Number of rows is the number of time zones in the region for the specified section.
    Region *region = [regions objectAtIndex:section];
    return [region.timeZoneWrappers count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    // The header for the section is the region name -- get this from the region at the section index.
    Region *region = [regions objectAtIndex:section];
    return [region name];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"MyReuseIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:MyIdentifier]];
    }
    Region *region = [regions objectAtIndex:indexPath.section];
    TimeZoneWrapper *timeZoneWrapper = [region.timeZoneWrappers objectAtIndex:indexPath.row];
    cell.textLabel.text = timeZoneWrapper.localeName;
    return cell;
}

Here's the link for the Apple documentation

like image 183
Levent Yıldız Avatar answered Sep 24 '22 22:09

Levent Yıldız