Reputation: 169
I've made a UITableView
in my ViewController
. In this table view I want to add my data from NSMutableArray
, but it does not seem to add the data. Any ideas?
This is the code I'm using.
- (void)viewDidLoad
{
scoreArray = [[NSMutableArray alloc] init];
NSDictionary *myVehicle = [[NSDictionary alloc] initWithObjectsAndKeys:@"Escape", @"name", @"SUV", @"type", nil];
[scoreArray addObject:myVehicle];
[super viewDidLoad];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = [[scoreArray objectAtIndex:indexPath.row] objectForKey:@"name"];
cell.detailTextLabel.text = [[scoreArray objectAtIndex:indexPath.row] objectForKey:@"type"];
return cell;
}
Upvotes: 1
Views: 761
Reputation: 33428
Based on your code you should be able to see one row. But here with no details, the problem is quite difficult to be identified.
Anyway, verify the following:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
correctly?Sample
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [scoreArray count];
}
The method - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
should be implemented if you need a number of sections greater than 1. By default returns one.
In addition, I would modify a little bit the viewDidLoad
method like the following.
- (void)viewDidLoad
{
[super viewDidLoad]; // call it before any further initialization
scoreArray = [[NSMutableArray alloc] init];
NSDictionary *myVehicle = [[NSDictionary alloc] initWithObjectsAndKeys:@"Escape", @"name", @"SUV", @"type", nil];
[scoreArray addObject:myVehicle];
}
Upvotes: 1
Reputation: 908
Make sure your .h file indicates that this is the delegate for the UITableView:
@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
Also make sure you implement the required datasource methods. In addition to what you have, you must have these:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
and
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [scoreArray count];
}
Upvotes: 0