2

I am experiencing a strange issue with this code.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }


    // Configure the cell...
    if (accounts != nil) {
        NSLog(@"Cell: %@", indexPath.row);
        cell.textLabel.text = [self.accounts objectAtIndex: indexPath.row];
    }
    else
    {
        NSLog(@"No cells!");
        [cell.textLabel setText:@"No Accounts"];
    }

    return cell;
}

My table view populates just fine, except all rows contain the first item in my NSMutableArray, accounts. I am logging the value of indexPath.row and it remains at (null) no matter how many values are in the array. Am I doing something wrong here?

2 Answers 2

3

I don't believe this! I am bonking myself on the head for not finding this sooner!

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [accounts count]; //<--This is wrong!!!
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1; // <--This needs to be switched with the error above
}

The above code was the reason why it was printing the same row in my array twice, rather than marching forward in my array.

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

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

This code is correct and produces the proper result. What a fuster cluck. ^^;

Sign up to request clarification or add additional context in comments.

1 Comment

I was about to say if you're cellForRowAtIndexPath is only getting called once, check your numberOfRows :)
2

Should be @"%i", indexPath.row not @"%@", indexPath.row

Also I recommend putting this at the top of your method:

NSUInteger row = [indexPath row];

Then your method looks like this:

// Cell Ident Stuff
// Then configure cell
if (accounts) {
    NSLog(@"Cell: %i", row);
    cell.textLabel.text = [self.accounts objectAtIndex:row];
}
else {
    NSLog(@"No accounts!");
    // Only setting for the first row looks nicer:
    if (row == 0) cell.textLabel.text = @"No Accounts"; 
}

It's good practice when dealing with table view methods. Try that.

1 Comment

I made this change and now the NSLog is reporting nothing but "Cell: 0". It's still not marching forward through my array.

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.