user10307250
user10307250

Reputation:

I can not get categories to appear in table view cells

The app launches without any technical problems, although I can not get the hardcoded categories to appear in table view cells from the .m file. The table view style is basic, and through that I thought I could reach the title sample through the .m file using:

self.items = @[@ {@"name" : @" Chores", @"Category" : @"Home"}].mutableCopy;

At first I thought the problem occurred because I forgot to set a reuse identifier. But fixing that problem didn't help. I get no warnings or errors in Xcode. I have not modified any other files except the ViewController.m and main.Storyboard files.

Code from the .m file

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic) NSMutableArray *items;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.items = @[@ {@"name" : @" Chores", @"Category" : 
@"Home"}].mutableCopy;

    //self.navigationItem.title = @"What2Do list";
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.items.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"TodoItemRow";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    NSDictionary *item = self.items[indexPath.row];

    cell.textLabel.text = item[@"name"];

    return cell;
}

@end

I expected "Chores" to show up in the table cell when I launch the app, but it will not appear.

Upvotes: 0

Views: 35

Answers (1)

L33MUR
L33MUR

Reputation: 4058

In the ViewController.h you need to set the Delegate of the table view.

@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>

Also you need to link the tableview to the viewcontroller as its delegate. The easiest way is adding it in the storyboard.

First drag + ctrl your table to the viewController Symbol on the storyboard.

enter image description here

On the list click on delegate.

Repeat again for dataSource.

enter image description here

Upvotes: 1

Related Questions