Couldn't set values in Custom Cell in UITableView from NSMutableArray

90 views Asked by At

Help me to get rid of with this dilemma that occurred yet when I tried to dequeued the cell (Custom Cell).Below are some steps and Indents that I did with my Project.

The very first is I drag and drop a UITableView in my ViewController and add the ViewController.h doing after this

@interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>

Then I made a Custom Cell with 3 UILabels and change the height of the Cell to 65.

After that I made a property in ViewController.m

@property (nonatomic,strong) NSMutableArray *myTodoTitles;

Then in method(ViewDidLoad) I did.

myTodoTitles = [NSMutableArray arrayWithCapacity:10];
[myTodoTitles addObject:@"Go for ride"];
[myTodoTitles addObject:@"Do University Assignments"];
[myTodoTitles addObject:@"Watch Show"];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[self.myTodoTitles count]-1 inSection:1];


[self tableView:self.myTodoTable cellForRowAtIndexPath:indexPath];

After that I just did these things in my ViewController.m

#pragma mark - Table view data source

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *myIdentifier = @"TodoCell";
    TodoCell *todoCell = (TodoCell*)[tableView dequeueReusableCellWithIdentifier:myIdentifier];

    todoCell.todoTitleLabel.text = [self.myTodoTitles objectAtIndex:indexPath.row];

    return todoCell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [myTodoTitles count];
}

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

But when I run the project it dequeued nothing. Please help

2

There are 2 answers

0
ReDetection On

Most likely that you have not connected your viewController to be the dataSource of your tableView. This could be done from Interface Builder or from the code. You can easily check it by adding self.myTodoTable.dataSource = self; at the very first of viewDidLoad method.

And also: what did you mean by `

[self tableView:self.myTodoTable cellForRowAtIndexPath:indexPath];` 

in viewDidLoad ? Seems like you wanted to do

[self.myTodoTable reloadData];
5
Rok Jarc On

There are to UITableView methods with similar name:

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier

and

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier
                           forIndexPath:(NSIndexPath *)indexPath

The first one will try to dequeue a reusable cell. If it returns nil you are responsible to create appropriate cell.

The latter one will always return a valid cell: you will have to register a certain class or NIB with that tableview beforehand though. Docs.

EDIT:

As ReDetection pointed out: first method will also return a valid cell as long as you had registered a proper class (or nib) with that tableview.

In your case that means that you should register TodoCell in viewDidLoad with your tableView.

If TodoCell is made without .xib:

[self.tableView registerClass:[ToDoCell class]
       forCellReuseIdentifier:@"TodoCell"];

Or if it is made with .xib.

[self.tableView registerNib:[UINib nibWithNibName:@"TodoCell" 
                                           bundle:nil]
     forCellReuseIdentifier:@"TodoCell"];

EDIT2:

Your code also seems to be missing the dataSource setting. Something like:

self.tableView.dataSource = self;

This will trigger initial reload.

You'd probably want to set a delegate (since your controller claims to adopt that protocol) in the same manner.