0

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

4
  • Can you post the entire .m file ? Commented Jun 14, 2015 at 18:19
  • Try this it's work for me "static NSString *myIdentifier = @"TodoCell"" Commented Jun 15, 2015 at 4:48
  • link hey user80755 you can see the whole .m file here Commented Jun 16, 2015 at 18:25
  • @user80755 hope you back to me fast after going through the file... :) Commented Jun 17, 2015 at 17:29

2 Answers 2

1

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];
Sign up to request clarification or add additional context in comments.

Comments

0

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.

5 Comments

Since iOS 6 dequeueReusableCellWithIdentifier: also returns valid cell as far as you have registered cell. The only difference in this case that the latter will produce nice log record with the actual indexPath in case smth went wrong and cell was not created.
Thanks! I missed that.
Hey.. #rokjarc where should I used self.tableView.dataSource = self; or should I also use self.tableView.delegate = self; please help and also thanks for your assistance ReDetection.
viewDidLoad would be appropriate: instead (and in the same place) of your [self tableView:self.myTodoTable cellForRowAtIndexPath:indexPath]; line actually.
By the way: is your tableView created programatically or with IB (xib, storyboard...). I'm just wondering if it is added to the view of your view-controller and if it has some kind of a frame set at all...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.