0

I pass an array called userArray which stores another array with strings. This is what I have so far but I know it's wrong. Can someone point me in the right direction?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    

    static NSString *CellIdentifier = @"Cell";    
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil) {
        cell = [[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
    }

    //Set Up Cell
    DataSingleton *sharedData = [DataSingleton sharedData];

    for (NSArray *array in sharedData.usersArray){
        cell.primaryLabel.text = [array objectAtIndex:1];
        cell.secondaryLabel.text = [array objectAtIndex:2];
        cell.profileImage = [UIImage imageNamed:@"111-user.png"];
        return cell;
    }
}
1
  • Is your return supposed to be inside the for loop ? Commented Feb 23, 2012 at 5:53

1 Answer 1

1

cellForRowAtIndexPath is a UITableViewDataSource method, and it asks the data for single cell only. So, you have to remove a loop, and set up a single cell at once, using indexPath.row as an array index in your DataSingleton

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *CellIdentifier = @"Cell";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil) {
        cell = [[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
    }

    //Set Up Cell
    DataSingleton *sharedData = [DataSingleton sharedData];

    NSArray *array = [sharedData.usersArray objectAtIndex:indexPath.row];
    cell.primaryLabel.text = [array objectAtIndex:1];
    cell.secondaryLabel.text = [array objectAtIndex:2];
    cell.profileImage = [UIImage imageNamed:@"111-user.png"];
    return cell;   
}

Also, you should implement tableView:numberOfRowsInSection: to return [[[DataSingleton sharedData] usersArray] count]

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

3 Comments

Can you explain further what you mean by returning the count? EDIT: NVM I SEE WHAT YOU MEAN
Did you mean: 'return [[[DataSingleton sharedData] usersArray] count];' ? either way I'm getting a null value
Ok, I realized I was switching views before it had a chance to write to my singleton but your inital post worked. Thank you very much.

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.