Okay I am creating a Table View using objective c, but the data source is not working correctly…
My error:
2012-06-02 20:14:39.891 Dot Golf Scoring[195:707] *** Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit/UIKit-1914.85/UITableView.m:6061
2012-06-02 20:14:39.895 Dot Golf Scoring[195:707] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
My Code:
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 16;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return @"Comments On Your Round";
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
cell.textLabel.text = @"Text Label";
return cell;
}
Why is the table view not getting filled with this fake data???
You are never initializing cell. Use this code:
You say you are a noob…. let me explain
First of try picking up the book:
The Big Nerd Ranch Guide
What you are thinking is that dequeueing is basically initializing right? NO! Dequeuing is basically nilling any cell that is not visible, aka you scroll past it. Therefore,
cell == nilwill be called in probably four situations (that I can think of):So, the identifier for dequeuing is like an ID. Then in the statement to see if
cellisnil, we initializecell, you can see the overriddeninitmethod:initWithStyle. This is just what type ofcellthere is, there are different types with different variables you can customize. I showed you the default. Then we use thereuseIdentifierwhich was the dequeuing identifier we said earlier. THEY MUST MATCH! I niltextLabeljust for better structure, in this case each cell has the same text so it won’t matter really. It makes it so the cell that dequeues comes back with the right customization you implemented. Then once cell is actually valid, we can customize.Also, you are using the same text for each cell. If you do want to have different text for each cell, familiar yourself with
NSArray. Then you could provide the arraycountinnumberOfRowsForSectionand then do something like this:Where
indexPathis theNSIndexPathargument provided in thecellForRowAtIndexPathmethod. Therowvariable is therownumber, so everything fits!Wow, that was a lot to take in right!
Now go stop being an objective-c noob and start reading some books!
For more info read:
Table View Apple Documentation