How to use NSMutableArray as a data source for my UITableView

I have an NSMutableArray in with data (hard-coded).

I implemented these two methods of TableView and in IB, I set the delegate and datasource

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

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

    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"] autorelease];
    }

    cell.textLabel.text = [myArray objectAtIndex:[indexPath row]];
    NSLog(@"Cell is %@", [myArray objectAtIndex:indexPath.row]);
    return cell;
}

Data will not be displayed in TableView. I started NSLog and I see that the data is in myArray.

I added NSLog to the code and it seems like the code is never executed. The question is, how is my array created?

Here is the code

- (id)init:(NSMutableArray *)theArray
{
    [super init];
    countryTable.delegate = self;
    countryTable.dataSource = self;

    UITapGestureRecognizer * tapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)] autorelease];
    [window addGestureRecognizer:tapRecognizer];

    myArray = [[NSMutableArray alloc] init]; 
    myArray = theArray;
    return self;
}
+5
source share
3 answers

Your base code is correct, you just need to have an array (which you did not specify here), so this will be one example:

NSMutableArray *myArray = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];

And you can NSLog:NSLog(@"Count: %i, Array: %@",[myArray count], myArray);

:, cell.textLabel.text

: 3, : (      "",      "",      "" )

, UITableViewDelegate UITableViewDataSource .

+5

, , .

, , countryTable, , InterfaceBuilder? , countryTable viewDidLoad. dataSource InterfaceBuilder.

, (myArray), countryTable.reloadData.

+1

-

cell.textLabel.text = [[myArray objectAtIndex:indexPath.row] retain];
0

All Articles